×
Clear all filters including search bar
Valeri Tandilashvili's PHP Notes
$name = "George";
$age = 25;
$weight = 75; //kg
$height = 1.85; //m
Variables used in expressions
echo "His name is ".$name."\n";
echo "He is ".$age." years old\n";
echo "He weighs around ".$weight."\n";
echo "he is ".$height."m tall \n";
$name = "George";
Integer
$weight = 85; //kg
Float
$height = 1.9; //m
Boolean
$male = true;
Array
$colors = ["Red", "Green", "Blue"];
// Instead of storing them in separate variables like this:
$color1 = "Red";
$color2 = "Green";
$color3 = "Blue";
gettype()
returns a type of a given variableecho gettype($my_name); // string
$
sign, followed by the name of the variable
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z
, 0-9
, and _
)
// Correct Syntax
$variable_name = "Value";
// Incorrect Syntax
Variable_Name = "Value";
$5variable_name = "Value";
$variable-name = "Value";
timezone
using php.ini
filedate.timezone = Asia/Tbilisi
Sets timezone
inside php script fileini_set('date.timezone', 'Asia/Tbilisi');
integer
and float
values to perform arithmetical operations.
Let's define two variables:
number1
and number2
and perform these operations$number1 = 40;
$number2 = 9;
Addition
echo $number1 + $number2 . "\n";
Subtraction
echo $number1 - $number2 . "\n";
Multiplication
echo $number1 * $number2 . "\n";
Division
echo $number1 / $number2 . "\n";
Modulus
echo $number1 % $number2 . "\n";
echo 'plain text'."\n";
Double quotes can parse variables
echo "Double quotes: $text"."\n";
Single quotes can not parse variables
echo 'Single quotes: $text'."\n";
Single quotes as well as double quotes can be concatenated with variables
echo 'Single quotes - '.$text." - Double quotes"."\n";
Text inside Single quotes can include double quotes as part of the text
echo 'The text includes "double" quotes as part of the text'."\n";
Text inside double quotes can include single quotes as part of the text
echo "Jane's surname is Smith"."\n";
echo can be used as a function
echo ('echo as a function')."\n";
echo can print number without quotes
echo 5 . $number;
echo
can take multiple parameters
echo 'one', 'two', 'three', "\n";
We can print multiple lines of text
echo 'Prints text
with multiple
lines';
// $variable = 'Value';
Another single line comment# $variable = 'Value';
Comment for multiple lines/*$variable = 'Value';
$second_variable = 'Value 2';
$third_variable = 'Value 3';*/
Inline comment$number = 5 /* - 5 */ + 5;
Hello world
echo "Hello world";
Prints the two words one by oneecho "Hello ";
echo "world";
Prints the variable $greeting
$greeting = "Hello world";
Echo $greeting;
Concatenates the string to the variable $greeting
$greeting = "hello world!";
Echo $greeting . " Let's get started!";
$d = date("D");
if($d == "Fri"){
echo "Have a nice weekend!";
} elseif($d == "Sun"){
echo "Have a nice Sunday!";
} else{
echo "Have a nice day!";
}
echo "\n";
Outputs Have a good morning!
if the current time is less than 10, and Have a good day!
if the current time is less than 20. Otherwise it will output Have a good night!
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
Prints Adult
If $age
is more than 18
, and Teenager
if the variable is less than 12. Otherwise it prints Child
$age = 20;
if ($age > 18) {
echo 'Adult';
} else if ($age > 12) {
echo 'Teenager';
} else {
echo 'Child';
}