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";
}
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
while (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*/
function
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 applicationOperator 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 true// Interesting example about if-elseif-else statement
$test = "STRING, NOT INTEGER OR FLOAT";
if ($test < 12) {
echo "child";
} else if ($test > 12 && $test < 18 ) {
echo "teen";
} else {
echo "adult";
}
// what do you think, what will be output, error or ?
// make that example on your PC if you are a begginer :)
if (condition1){
//code to be executed if condition1 is true
} elseif (condition2){
//code to be executed if condition1 is false and condition2 is true
} elseif (condition3){
//code to be executed if condition2 is false and condition3 is true
....
} else{
//code to be executed if all given conditions are false
}
$x = 10;
if ($x > 0) {
echo "$x is positive\n";
} elseif ($x < 0 ) { // if $x equals negative number
echo "$x is negative!\n";
} else { // if $x = 0;
echo "$x is neither positive nor negative \n";
}