Conditional statements are used to perform different actions based on different conditions.
PHP supports different types of conditional statements
PHP Conditional Statements & Loops
- if statement: if statement executes some code if one condition is true.
- if…else statement: if-else statements allow you to display output in both the condition(if a condition is a true display some message otherwise display another message).
- if…elseif…else statement: if…elseif…else statement executes different codes for more than two conditions.
- switch statement: selects one of many blocks of code to be executed.
PHP if Statement
The if statement executes some code if one condition is true.
Syntax:
if(condition){ code to be executed if condition is true; }
Example :
<?php $a=5; if($a<10){ echo "Hello, have a good day!"; } ?>
Output “Hello, have a good day!” because of the condition in if the statement is true i.e. 5<10
PHP if…esle Statement
The if-else statements allow you to display output in both the condition(if a condition is a true display some message otherwise display another message). PHP Conditional Statements & Loops
Syntax
if (condition) { code to be executed if condition is true; }else { code to be executed if conditions is false; }
Example :
<?php $a = 15; if ($a< 10) { echo "value is less than 10"; } else{ echo "value is greater than 10"; } ?>
Output “value is less than 10” if the value of $a is less than 10, otherwise it will output “value is greater than 10” .
PHP if…esleif…else Statement
The if…elseif…else statement executes different codes for more than two conditions.
Syntax
if (condition) { code to be executed if this condition is true; } elseif (condition) { code to be executed if first condition is false and this condition is true; } else { code to be executed if all conditions are false; }
Example :
<?php $a = 15; if ($a< 10) { echo "value is less than 10"; } elseif ($a > 10) { echo "value is greater than 10"; } else { echo "value is negative"; } ?>
Output “value is less than 10” if the value of $a is less than 10, and “value is greater than 10″ if the value of $a is greater than 10. Otherwise, it will output ” value is negative”. PHP Conditional Statements & Loops
PHP switch Statement
The PHP switch statement is used to select one of many blocks of code to be executed.
Syntax
switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break; ... default: code to be executed if n is different from all labels; }
How it works: First we have a single expression, that is evaluated once. Then the value of this expression is compared with the values for each case in the structure. If the match found, the block of code associated with that case is executed. A break is used to prevent the code from running into the next case automatically. The default statement is used if no match is found.
Example :
<?php $favcolor = "red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, nor green!"; } ?>
PHP Loops
Loops are used when you want the same block of code to run over and over again a certain number of times. So instead of adding several equal lines of code again and again in a script, we can use loops.
Loops are used to execute the same block of code again and again, as long as a certain condition is true.
We have the following loops in PHP:
- The while loop: execute/loops a block of code as long as the specified condition is true.
- The do…while loop: execute/loops through a block of code once, and then repeats the loop as long as the specified condition is true.
- The for loop: execute/loops through a block of code a specified number of times.
- The foreach loop: execute/ loops through a block of code for each element in an array.
Now we will see an example of each loop.
PHP while Loop
The while loop executes a block of code as long as the specified condition is true.
Syntax
while (condition is true) { code to be executed; }
Example :
The example below displays the number from 1 to 5:
<?php $a = 1; while($a <= 5) { echo "The number is: $a <br>"; $a++; } ?>
Example Explanation :
$a=1; – initialise the loop counter ($a), and set the start value to 1
$a<=5 – Continue the loop as long as $x is less than or equal to 5
$a++; – Increase the loop counter value by 1 for each iteration
Example :
The example below displays the square from 1 to 100:
<?php $a = 1; while($a <= 100) { $result=$a*$a; echo "The square of $a is: $result <br>"; $a++; } ?>
PHP do-while Loop
The do…while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.
Syntax
do { code to be executed; } while (condition is true);
Example :
<?php $a = 1; do { echo "The number is: $a <br>"; $a++; } while($a <= 5); ?>
Example Explanation
The example above first sets a variable $a to 1 ($a = 1). Then, the do-while loop will write some output, and then increment the variable $a with 1. Then the condition is checked (is $a less than, or equal to 5?), and the loop will continue to run as long as $a is less than, or equal to 5.
PHP for Loop
The for loop is used when you know in advance how many times the script should run.
Syntax
for (init counter; test counter; increment counter) { code to be executed for each iteration; }
Parameters:
- 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.
Example :
<?php for ($a = 0; $a <= 10; $a++) { echo "The number is: $a <br>"; } ?>
Example Explanation
$x = 0; – Initialize the loop counter ($x), and set the start value to 0
$x <= 10; – Continue the loop as long as $x is less than or equal to 10
$x++ – Increase the loop counter value by 1 for each iteration
PHP foreach Loop
The foreach loop works only on arrays and is used to loop through each key/value pair in an array.
Syntax
foreach ($array as $value) { code to be executed; }
For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one until it reaches the last array element.
Example :
<?php $colors=array("red","green","blue","yellow"); foreach($colors as $value){ echo "$value <br>"; ?>
The above example will output the values of the given array($colors).
The following example will output both the keys and the values of the given array ($color):
<?php $age = array("Apple"=>"red", "Mango"=>"yellow", "Blackberries"=>"purple"); foreach($age as $x => $val) { echo "$x = $val<br>"; } ?>