Conditional statements in PHP
Conditional statements: if, if-else, switch, The ? Operator
In PHP, conditional statements execute code based on the truth or false of conditions. PHP allows the following conditional statements,
If statements.
If……. Else statements.
Switch statements.
The ? operator.
If statements:
If conditions allow us to execute our program with a single true condition.
Syntax:
If(condition)
{
//to be executed block of code if the condition is true.
}
Flowchart:
Example 1: Simple program with PHP.
<?php
$a=5;
if($a%2==0)
{
echo "Number is Even";
}
?>
OUT-PUT
Number is Even
Example 2: Program with PHP and HTML.
<html>
<body>
<form method="POST" action="p1.php">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
$a=$_POST['fname'];
if($a%2==0)
{
echo "Number is Even";
}
?>
</body>
</html>
OUTPUT
If…….else statements:
If…….else conditions execute the program based on both true and false conditions. If the condition is true, it will display its true block; if the condition is false, it will display its false block.
Syntax:
If(condition)
{
//to be executed block of code if the condition is true.
}
Else
{
//to be executed block of code if the condition is false.
}
Example 1: Simple program with PHP.
<?php
$a=5;
if($a%2==0)
{
echo "Number is Even";
}
else
{
echo "Number is Odd";
}
?>
OUT-PUT
Number is Odd
Example 2: Program with PHP and HTML.
<html>
<body>
<form method="POST" action="p1.php">
Number : <input type="text" name="fname">
<input type="submit">
</form>
<?php
$a=$_POST['fname'];
if($a%2==0)
{
echo "Number is Even";
}
else
{
echo "Number is Odd";
}
?>
</body>
</html>
OUTPUT
If …….elseif…..else (Nested if---else)
A nested if-else statement executes statements within statements. If the first condition is true, then only the second condition is executed; otherwise, the third condition will be executed.
Syntax:
If(condition 1)
{
If(condition 2)
{
//to be executed block of code if the condition is true.
}
Else
{
//to be executed block of code if the condition is false.
}}
Elseif (condition 3)
{
{
//to be executed block of code if the condition is true.
}
Else
{
//to be executed block of code if the condition is false.
}
Example 1: Simple program with PHP.
<?php
$a=5;$b=25;$c=115;
if($a>$b)
{
if($a>$c)
{
echo "a number is large";
}
else{
echo "c number is large";
}}
elseif($b>$c)
{
echo "b number is large";
}
else{
echo "c number is large";
}
?>
OUTPUT
c number is large
Example 2: Program with PHP and HTML.
<html>
<body>
<form method="POST" action="p1.php">
Number 1: <input type="text" name="n1"></br>
Number 2: <input type="text" name="n2"></br>
Number 3: <input type="text" name="n3"></br></br>
<input type="submit"></br>
</form>
<?php
$a=$_POST['n1'];
$b=$_POST['n2'];
$c=$_POST['n3'];
if($a>$b)
{
if($a>$c)
{
echo "first number is large";
}
else{
echo "Thirs number is large";
}}
elseif($b>$c)
{
echo "Second number is large";
}
else{
echo "Third number is large";
}
?>
</body>
</html>
OUTPUT
switch statement:
in PHP, switch case is a multiple branching statement. It compares the value of the case expression or variable inside the switch and executes a block when a match is found. Switch provides a default statement; when the case is not found inside the switch, the default block is executed.
Syntax:
{
case 1:
//block1;
break;
case 2:
// block2;
break;
... ... ...
... ... ...
case N:
// block3;
break;
default:
// defaultblock;
break;
}
Flowchart of switch case statement
Example 1: Simple program with PHP.
<?php
$a=1;$b=25;$c=15;
echo "value of first number ",$b,"</br>";
echo "value of second number",$c,"</br>","</br>";
switch($a)
{
case 1:
echo "Addition",$b+$c,"</br>";
break;
case 2:
echo "Subtaction",$b-$c,"</br>";
break;
}
?>
Example 2: Program with PHP and HTML.
<html>
<body>
<form method="POST" action="p1.php">
Number 1: <input type="text" name="n1"></br>
Number 2: <input type="text" name="n2"></br>
Number 3: <input type="text" name="n3"></br></br>
<input type="submit"></br>
</form>
<?php
$a=$_POST['n1'];
$b=$_POST['n2'];
$c=$_POST['n3'];
if($a>$b)
{
if($a>$c)
{
echo "first number is large";
}
else{
echo "Thirs number is large";
}}
elseif($b>$c)
{
echo "Second number is large";
}
else{
echo "Third number is large";
}
?>
</body>
</html>
OUTPUT
Number 1:
The ? operator
The ? operator is used in PHP in two ways
First: The conditional operator is also called the ternary operator.
Second: It comes with Null coalescing:
conditional operator (? :)
It is similar to an if-else statement; an if-else executes the block based on a true or false condition. Ternary Conditional statements provide the true or false section within “:” and “?” operators.
Syntax:
Exp1 ? exp2 ?exp3;
Example:
<?php
$a=5;
echo ($a%2==0)? "Number is Even": "Number is Odd";
?>
Null coalescing:
Syntax:
Exp1 ?? exp2 ;
Example:
<?php
$a=5;
$b=Null;
echo $a??$b;
?>
Looping statements: while Loop, do...while Loop, for Loop.
Content | Definition |
What are looping statements? | Looping statements are those statements that execute repeatedly whenever a given condition is met. |
Types: | There are two types of looping control statements, which are Entry control and Exit control. |
Entry control: | In entry control, looping statements, the condition is checked before each iteration. Example: While loop and for loop |
Exit control: | In exit control, looping statements, the condition is checked after each iteration. Example: Do… while loop |
Key content | Each looping statement involves three things: Initialization, condition, and Increment/decrement. |
Initialization | It involves declaring and initializing a loop control variable. |
Condition | It is an evaluated expression before the iteration of the loop. |
Increment/ decrement | It increases or decreases the move to fulfil the termination condition. |
While loop
Content | Definition |
While loop | While loop is an entry control loop, in which the condition is checked before each iteration. While block executes only when the condition is true; otherwise loop terminates. If the condition is false block won't be executed. |
Syntax: | While(condition) { //To be executed block; } |
Flowchart:
Example:
<html>
<head><title>my page</title></head>
<body>
<h3> </h3>
<?php
$a=1;
while ($a<=5)
{
Echo "welcome</br>";
@$a++;
}
?>
</h3>
</body>
</html>
For Loop
Content | Definition |
for loop | A for loop is an entry control loop, in which the condition is checked first before the iteration begins. “For” block executes only when the condition is true; otherwise, it will be denied. For. For loop involves three conditions at a time, like (initialization, conditions, increment/decrements) |
Syntax: | for(initialization, conditions, increment/decrements) { //To be executed block; } |
Flowchart
Example:
<?php
for($a=1;$a<=10;$a++)
echo $a,"</br>";
?>
OUTPUT
1
2
3
4
5
6
7
8
9
10
Comments
Post a Comment