PHP unit 2
PHP unit 2
Conditional Statement
Conditional statements are used to perform different actions based on different conditions.
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) {
Example - 1 Example – 2
<?php <?php
?> }
?>
Syntax
if (condition) {
} else {
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!";
}
}
?>
?>
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.
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