// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
// You can also use comments to leave out parts of a code line
$a = 2 /* - 1 */ + 4;
echo
statement can be used to print the string,
multi-line strings, escaping characters, variable, array, etc.
• printing string
echo "Hello friends!\n";
• printing multi line string
echo "How are you ?
this is multi line
text !\n";
• printing escaping characters
echo "Hello \"Hello\" Hello\n";
• printing variable value ( only DOUBLE quetes can parse variables )
$num= 17;
echo "num = $num\n";
// or => echo "num = ".$num."\n";
// or => echo 'num = '.$num."\n";
• printing number without quotes
echo 10;
• printing multiple parameters
echo "red"," yellow"," black";
• echo can be used with or without parentheses: echo(), and echo.
echo ("function\n");
• Text inside double quotes can include single quotes as part of the text.
The opposite is also possible.
echo "My name is 'Anton'\n";
echo 'My name is "Anton"';
➤ PHP print
statement works exactly the same way except
that the print statement can only output one parameter, and always returns 1.
• returns 1
$result = print"Hello\n";
echo "result = $result"; // output: 1
• print
can take only one argument
print "hello","world"; // error
echo
statement considered marginally faster than the print
statement since it doesn't return any value.
• print
always returns an integer value, which is 1.
$result = print 10;
echo $result; // output: 1
• Using print
, we cannot pass multiple arguments.
print 10,20,30; // error
$str = "string"; // "hello" "Gamarjoba" 'levani' and so on.
• Integer
$int_num = 10; // -12 400 9999 -123421 90 and so on.
• Float
$float_num = 2.5; // -1.5 20.5 150.7 and so on.
• Boolean
$human = true; // equals 1
$robot = false; // equals 0
• Array
$laptops = array("Dell","Lenovo","Hp",1,2,3);
• constant
Constants are similar to variables except that they cannot be changed or undefined after they've been defined.
Begin the name of your constant with a letter or an underscore.
To create a constant, use the define() function. (no $ sign before the constant name):
• 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
define(name, value, case-insensitive)
// define constant
define("PI",3.141592);
echo PI;
I will discuss additional data types later, in a new note!$arr = []; // or $arr = array();
$arr[0] = "Levani";
$arr[1] = "Nika";
$arr[2] = "Giorgi"; // or $arr = ["levani","Nika","Giorgi"]
$arr[] = "Saba"; // add last element in arr
echo "I am $arr[0] \n"; // output: I am Levani
• Associative array — An array where each key has its own specific value. In the following example the array uses keys instead of index numbers:
$ages = [
"John" => 15,
"Michael" => 25,
"Levani" => 19,
"Mads" => 57
];
// or
// $ages["John"] = "15";
// $ages["Michael"] = "25";
// $ages["Levani"] = "19";
// $ages["Mads"] = "57";
echo "I am ".$ages["Levani"]." years old \n"; // output: I am 19 years old
• Multidimensional array — An array containing one or more arrays within itself.
$laptop = [
"Lenovo" => [
"price" => 1200,
"color" => "white",
"storage" => "300 Gb",
],
"Dell" => [
"price" => 1000,
"color" => "black",
"storage" => "500 Gb",
],
];
echo "My laptop price is ".$laptop["Dell"]["price"].".\n"; // output: My laptop price is 1000.
\n
represents the newline (linefeed)
echo "Co\nding\n";
// output: Co
// ding
• \t
represents the tab
echo "Co\tding\n";
// output: Co ding
$x++; // equivalent to $x = $x+1;
$x--; // equivalent to $x = $x-1;
• Increment and decrement operators either precede or follow a variable.$x++; // post-increment
$x--; // post-decrement
++$x; // pre-increment
--$x; // pre-decrement
• The difference is that the post-increment returns the original value before it changes the variable,
while the pre-increment changes the variable first and then returns the value.
For instance:
$a = 2; $b = $a++; // $a=3, $b=2
$a = 2; $b = ++$a; // $a=3, $b=3
$a = 2; $b = $a--; // $a=1, $b=2
$a = 2; $b = --$a; // $a=1, $b=1
$x = 10;
$y = $x;
echo $y; // output: 10
Assignment Same as... Description
$x = $y $x = $y The left operand gets set to the value of the expression on the right
$x += $y $x = $x + $y Addition
$x -= $y $x = $x - $y Subtraction
$x *= $y $x = $x * $y Multiplication
$x /= $y $x = $x / $y Division
$x %= $y $x = $x % $y Modulus
For instance:
$x = 15;
$x %= 4; // output: 3
$x = 5;
$x *= 6; // output: 30
Operator Name Example Result
== Equal $x == $y Returns true if $x is equal to $y
=== Identical $x === $y Returns true if $x is equal to $y, and they are of the same type
!= Not equal $x != $y Returns true if $x is not equal to $y
<> Not equal $x <> $y Returns true if $x is not equal to $y
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same type
> Greater than $x > $y Returns true if $x is greater than $y
< Less than $x < $y Returns true if $x is less than $y
>= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y
<= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y
For instance:// Equal
$x = 50;
$y = "50";
var_dump($x == $y); // returns TRUE because values are equal
echo "\n";
// Identical
$x = 50; // integer type
$y = "50"; // string type
var_dump($x === $y); // returns false because types are not equal
echo "\n";
// Not equal
$x = 50;
$y = "50";
var_dump($x != $y); // returns false because values are equal
echo "\n";
// Not equal. Both operators (!= / <>) give the same output. The only difference is that '<>' is in line with the ISO standard while '!= ' does not follow ISO standard.
$x = 50;
$y = "50";
var_dump($x <> $y); // returns false because values are equal
echo "\n";
// Not identical
$x = 50;
$y = "50";
var_dump($x !== $y); // returns true because types are not equal
echo "\n";
// Greater than
$x = 100;
$y = 50;
var_dump($x > $y); // returns true because $x is greater than $y
echo "\n";
// Less than
$x = 10;
$y = 50;
var_dump($x < $y); // returns true because $x is less than $y
echo "\n";
// Greater than or equal to
$x = 10;
$y = 10;
var_dump($x >= $y); // returns true because $x is greater than or equal to $y
echo "\n";
// Less than or equal to
$x = 10;
$y = 10;
var_dump($x <= $y); // returns true because $x is less than or equal to $y
echo "\n";
$name = "Mads";
then we must need to use $name. $NAME will not work.
$name = "Mads";
echo $NAME; // output: error
If we defined function name in lowercase, but calling them in uppercase it will work. For instance, If we define function sum() {} then calling SUM() will also work.function sum () {
echo "10 + 10 = 20 \n";
}
SUM(); // output: 10 + 10 = 20