Operator Name Example Result
and And $x and $y True if both $x and $y are true
or Or $x or $y True if either $x or $y is true
xor Xor $x xor $y True if either $x or $y is true, but not both
&& And $x && $y True if both $x and $y are true
|| Or $x || $y True if either $x or $y is true
! Not !$x True if $x is not true
For instance:
$x = 10;
$y = -10;
// Operator - and
if ( $x > 5 and $y > -15 ) {
echo "True: $x > 5"."\n";
echo "True: $y > -15"."\n\n";
}
// Operator - or
if ( $x > 100 or $x > -1 ) {
echo "False: $x > 100"."\n";
echo "True: $x > -1"."\n\n";
}
// Operator - xor
if ($x == 10 xor $y == -10 ) {
// both conditional statements are true!
} else {
echo "Both is true \n\n";
}
// Operator - && (same as - and)
if ($y <= 0 && $y == -10 and $y != 10) {
echo "True: $y <= 0\n";
echo "True: $y = -10\n";
echo "True: $y != 10\n\n";
}
// Operator - || (same as - or)
if ($x == 1 or $x == 10){
echo "True: $x = 10\n";
echo "False: $x = 1\n\n";
}
$x = 0;
// Operator - !
if (!$x) {
echo "$x is false";
}
➤ The number 0 is considered to be false and all other numbers are considered to be truefunction
functionName() {
code to be executed;
}
functionName() // Calling a function
For instance:
function printMyName() {
echo "My name is Levani :))\n";
}
printMyName(); // calling a function
➤ Function Arguments
Information can be passed to functions through arguments. An argument is just like a variable.
Arguments are specified after the function name, inside the parentheses.
You can add as many arguments as you want, just separate them with a comma.
For instance:
// Function with parameters
function printInfo($firstName,$lastName,$age){
echo "Hello,my name is $firstName $lastName, I am $age years old :))\n";
}
// Arguments
printInfo("Levani","Makhareishvili",19);
$name = "Mads";
$lastName = "Mikkelsen";
$age = 56;
printInfo($name,$lastName,$age);
➤ Note the difference between parameters and arguments:
• Function parameters are the names listed in the function's definition.
• Function arguments are the real values passed to the function.
• Parameters are initialized to the values of the arguments supplied.
➤ The advantages of using functions are:
• Functions reduces the repetition of code within a program
• Functions makes the code much easier to maintain
• Functions makes it easier to eliminate the errors
• Functions can be reused in other applicationwhile (condition is true) {
code to be executed;
}
For instance:
// outputs the numbers from 1 to 10
$x = 1;
while($x <= 10) {
echo "The number is: $x \n";
$x++;
}
// $x = 1; - Initialize the loop counter ($x), and set the start value to 1
// $x <= 10 - Continue the loop as long as $x is less than or equal to 5
/* $x++; - Increase the loop counter value by 1 for each iteration.
Otherwise, the condition always will be true and the loop will run endlessly*/
do {
code to be executed;
} while (condition is true);
For instance:
$x = 1;
do {
echo "The number is: $x \n";
$x++;
} while ($x <= 2);
/* outputs The number is: 1
The number is: 2 */
$x = 10;
do {
echo "The number is: $x \n";
$x++;
} while ($x <= 2);
// outputs The number is: 10
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}
➤ Parameters (divided by ; ):
• init counter: Initialize the loop counter value
• test counter: Evaluated for each loop iteration. If it evaluates to TRUE,
the loop continues. If it evaluates to FALSE, the loop ends.
• increment counter: Increases the loop counter value
For instance:
// outputs the numbers from 1 to 10
for ($i = 0; $i <= 10; $i++) {
echo "The number is: $i \n";
}
// Without first parameter
$i = 0;
for (; $i <= 10; $i++) {
echo "The number is: $i \n";
} // outputs the numbers from 1 to 10
// Without first and third parameter
$i = 0;
for (; $i <= 10;) {
echo "The number is: $i \n";
$i++; // we got the while loop syntax :))
} // outputs the numbers from 1 to 10
// We can make a for loop infinite without using these three parameters
for ( ; ; ){
echo "infinite \n";
}
foreach ($array as $value) {
code to be executed;
}
For instance:
// array - $country
$country = ["Georia","Ukraine","Spain"];
foreach ($country as $value) { // Instead of name $value we can write any name
echo "$value \n";
}
/* outputs: Georia
Ukraine
Spain
*/
// array - numbers
$numbers = array("one" => 1 , "two" => 2 , "three" => 3 );
foreach($numbers as $n => $value) {
echo "$n = $value \n";
}
/*outputs: one = 1
two = 2
three = 3
*/
// also, we can print only keys/values
foreach($numbers as $n => $value){
echo "$n \n"; // if we need value of key, we must whrite $value
} // keys are "one","two","three" and values are 1,2,3
/*outputs: one
two
three
*/