Control Flow in PHP


Share

Conditionals is Control flow in PHP.
They asked Question about centain thing and respond to the in different way.
They make it easy to create different output each time a page is viewed.
So, conditionals are central to dynamic web pages.



PHP has three nonlooping conditionals


1) If Statement

If follow only TRUE statement.

<?php
if ($class_students < 40) 
{
$students   = 10; 
$class_students += $students;
} 
?> 

If Else Statement

If Else follow both TRUE and FALSE statement

<?php
if ($class_students < 40) 
{
$students   = 10; 
$class_students += $students;
} else{
echo "Seats not available";
}
?>

Elseif Statement

There are also times when you want a number of different possibilities to occur,
based upon a sequence of conditions. You can achieve this using the elseif statement.

<?php
if ($class_students < 40) 
{
$students = 10; 
$class_students += $students;
} 
elseif ($class_student >52)
{
$students = 2;
$class_students += $students;
}
else
{
echo "Seats no available";
}
?>

2) Switch Statement

The switch statement is useful in cases in which one variable or the result of an
expression can have multiple values, which should each trigger a different function.

<?php
switch ($Numbers)
{
case "1":
echo "You selected Home"; 
break;   
case "2":      
echo "You selected About";  
break;  
case "3":       
echo "You selected News";     
break;    
case "4":       
echo "You selected Login";      
break;  
case "5":        
echo "You selected Links"; 
break; 
}
?>

3)The ? Operator

The ? operator(ternary operator) is passed an expression that it must evaluate,
along with two statements to execute:
one for when the expression evaluates to TRUE, the other for when it is FALSE.

<?php
echo $fuel <= 1 ? "Fill tank now" : "There's enough fuel";
?>

One another example using TRUE, FALSE statement

<?php
$enough = $fuel <= 1 ? FALSE : TRUE;
?>