PHP - UNIT - 2
Conditional Statement
Conditional statements are used to perform different actions based on different conditions.
In PHP we have the following conditional statements:-
if statement
if...else statement
if...elseif...else statement
switch statement
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 - 1 Example – 2
<?php <?php
if (5 > 3) { $num = 14;
echo "Have a good day!"; if ($num < 20) {
} echo "Hello PHP";
?> }
?>
Output- Have a good day! Output- Hello PHP
if….else Statement
The if...else statement executes some code if a condition is true and another code if that
condition is false.
Syntax
if (condition) {
// code to be executed if condition is true;
} else {
// code to be executed if condition is false;
Example - 1
<?php
$t = 5;
if ($t < 20) {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
Output- Have a good day!
Example – 2
<?php
if (100 > 10) {
echo "Have a good morning!";
} else {
echo "Have a good night!";
}
?>
Output- Have a good morning!
if...elseif...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 - 1 <?php
<?php $t = 10;
$t = 100; if ($t < 20) {
if ($t < 20) { echo "Have a good morning!";
echo "Have a good morning!"; } elseif ($t > 20) {
} elseif ($t > 200) {
echo "Have a good day!";
echo "Have a good day!"; } else {
} else { echo "Have a good night!";
echo "Have a good night!";
}
}
?>
?>
Example – 2 OUTPUT - Have a good morning!
OUTPUT- Have a good night!
switch Statement
The switch statement is used to perform different actions based on different conditions.
This statement is used to select one of many blocks of code to be executed. \
Syntax
switch (expression) {
case label1:
//code block
break;
case label2:
//code block;
break;
case label3:
//code block
break;
default:
//code block
}
i.e
expression is an VALUE.
case label1 is an CONDITION.
Break means breaks out switch block. This stop the execution of more code and
no more cases will be tested.
Example-1
<?php
$weekday = 4;
switch ($weekday)
case 1:
echo "Monday";
break;
case 2:
echo "Tuesday";
break;
case 3:
echo "Wednesday";
break;
case 4:
echo "Thrusday";
break;
case 5:
echo "Friday";
break;
case 6:
echo "Saturday";
break;
case 7:
echo "Sunday";
break;
default:
echo "INVALID";
?>
OUTPUT- Thrusday
PHP LOOPS
Loops are used to execute the same block of code again and again, as long as a certain
condition is true.
i.e
To start loop and assign a value.
If condition is true it prints a statement and inc or dec the value and again start the
another Condition.
In PHP, we have the following loop types:
while loop
do...while loop
for loop
foreach loop
while Loop
The while loop executes a block of code as long as the specified condition is true.
It execute the code repeatedly until condition gets false.
SYNTAX-
while (condition)
// code………..
While loop is called as ENTRY CONTROL LOOP because condition is checked before
entering the loop body.
st
Means 1 condition is checked and if condition is true, the block of code will be executed.
EXAMPLE - 1
<?php
$i = 1;
while ($i < 6) {
echo "$i<br>";
$i++;
}
?>
OUTPUT-
1
2
3
4
5
for Loop
Loops through a block of code a specified number of times.
This loop is used if no of iterations are executed.
Syntax
for (initialization, condition, inc/dec) {
// code block
}
Example-
<?php
for ($x = 1; $x <= 10; $x++) {
echo "$x. hello php <br>";
}
?>
OUTPUT-
1. hello php
2. hello php
3. hello php
4. hello php
5. hello php
6. hello php
7. hello php
8. hello php
9. hello php
10. hello php