Conditional Statements
Conditional statements are used to perform different actions based on different conditions.
In PHP we have the following conditional statements:
- if statement – use this statement to execute some code only if a specified condition is true
- if..else statement – use this statement to execute some code if a condition is true and another code if the condition is false
- if..elseif…. else statement – use this statement to select one of several blocks of code to be executed
- switch statement – use this statement to select one of many blocks of code to be executed
i. The if Statement
Use the if statement to execute some code only if a specified condition is true.
Synatx:-
if (condition) code to be executed if condition is true;
The following example will output “Have a nice weekend!” if the current day is Friday:
1 2 3 4 5 6 7 8 |
<html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; ?> </body> </html |
ii. if…else Statement
Use the if… else statement to execute some code if a condition is true and another code if a condition is false.
syntax
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
Example
The following example will output “Have a nice weekend!” if the current day is Friday, otherwise it will output “Have a nice day!”:
1 2 3 4 5 6 7 8 9 10 11 |
<?php $num=10; if($num%2==0) { echo "$num is even number"; } else { echo "$num is odd number"; } ?> |
output :– 10 is even number
iii.The if…elseif…. else Statement
Use the if….elseif…. else statement to select one of several blocks of code to be executed.
Syntax
if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
Example
The following example will output “Have a nice weekend!” if the current day is Friday, and “Have a nice Sunday!” if the current day is Sunday. Otherwise it will output “Have a nice day!”:
1 2 3 4 5 6 7 8 9 10 11 |
<html> <body> <?php $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!"; ?> </body> </html> |
Conditional statements are used to perform different actions based on different conditions.
iv. PHP Switch Statement
In php switch statement is used to perform different actions based on different conditions. In others word PHP switch statement is used to execute one statement from multiple conditions. It works like PHP if-else-if statement.
Syntax
switch(expression){
case value1:
//code to be executed
break;
case value2:
//code to be executed
break;
……
default:
code to be executed if all cases are not matched;
}
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php $num=5; switch($num){ case 1: echo("Monday"); break; case 2: echo("Tuesday"); break; case 3: echo("Wednasday"); break; case 4: echo("Thrusday"); break; case 5: echo("Friday"); break; case 6: echo("Sateday"); break; case 7: echo("Sunday"); break; default: echo("Please enter number between 1 to 7"); } ?> |
For Loop
The for loop is used when you know in advance how many times the script should run. In php for loops execute a block of code specified number of times.
Syntax
for (initilation; condition; increment/decrement)
{
code to be executed;
}
Example
1 2 3 4 5 6 |
<?php for ($i = 0; $i <= 10; $i++) { echo "$i <br>"; } ?> |
Output
0 1 2 3 4 5 6 7 8 9 10
While Loop
The while loop is used when you don’t know how many times the script should run. while loops execute a block of code while the specified condition is true same like for loop.
Syntax
while(condition)
{
//code to be executed
}
Example
1 2 3 4 5 6 7 8 |
<?php $n=0; while($n<=10) { echo "$n<br/>"; $n++; } ?> |
Output
0 1 2 3 4 5 6 7 8 9 10
do..while Loop
do..while loop is used where you need to execute code at least once. 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);
Example
1 2 3 4 5 6 7 8 |
<?php $x = 0; do { echo "$x <br>"; $x++; } while ($x <= 10); ?> |
Output
0 1 2 3 4 5 6 7 8 9 10