<?php
$d = date("D");
if ($d == "Sun"){
echo "I don't work". ".";
}
?>
If the current hour is less than 20, the code will output "I am going to sleep"
<?php
$t = date("H");
if ($t < "22"){
echo " I am going to sleep";
}
?>
<?php
$t = date("H");
if ($t < "18") {
echo "I watch TV";
} else {
echo "I play a game";
}
?>
// Correct Syntax ($variable_name = "Value";)
$my_car = "BMW";
// Incorect Syntax
my_car = "BMW";
$7my_car = "BMW";
$my&car = "BMW";
<?php
$name = "Tornike"; // global scope
function greeting() {
echo "Hello " . $name . "!"; // using $name will generate an error
}
greeting();
echo "Hello" . $name . "!"; // using $name will generate output (Hello Tornike!)
?>
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:
<?php
function greeting() {
$name = "Tornike"; // local scope
echo "Hello " . $name . "!"; // using $name inside the function will generate output (Hello Tornike!)
}
greeting();
echo "Hello" . $name . "!"; // using $name outside the function will generate an error
?>
<?php
$x = 70;
$y = 7;
function myNum {
global $x, $y;
$y = $x + $y;
}
myNum();
echo $y; // output is 77
?>
<?php
$x = 70;
$y = 7;
function myNum {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myNum();
echo $y; // output is 77
?>
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest(); // output is 1
myTest(); // output is 2
myTest(); // output is 3
?>
Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.
Note: The variable is still local to the function.define(name, value, case-insensitive)
Parameters:
name: Specifies the name of the constant;
value: Specifies the value of the constant;
case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false;
The example below creates a constant with a case-sensitive name:
<?php
define("georgian_player", "Kvicha Kvaratskhelia");
echo georgian_player;
// Outputs "Kvicha Kvaratskhelia"
?>
The example below creates a constant with a case-insensitive name:
<?php
define("georgian_player", "Kvicha Kvaratskhelia", true);
echo Georgian_Player;
// Outputs "Kvicha Kvaratskhelia"
?>
No dollar sign ($) is necessary before the constant name.