PHP IF Statement

PHP IF Statement

  • PHP
  • 5 mins read

In this tutorial, you will learn how to use IF statements in PHP to make decisions.

• If..else statement
• If..elseif..elseif..else statement

If else Statement

This statement can be used to execute a set of codes based on a condition. General Syntax of if..else statement is given below:

if(expression1)
{
Statement1;
Statement2;
.
.
}
else
{
Statement11;
Statement22;
.
.
}

Here if the expression1 is evaluated to true then Statement1,Statement2,.. will be executed.
If the expression1 is evaluated to false then Statement11, Statement22..will be executed.

1. Program on If-else statement

<html>
<body>
<?php
$a=90;
$b=80;
if($a>$b)
echo “The first number is maximum”;//Statement1
else
echo “The second number is maximum”;//Statement2
?>
</body>
</html>

Output: The first number is maximum(Here $a is greater than $b so $a>$b will be evaluated and return true as 1.So Statement1 will be executed)

2. Program on If-else statement

<html>
<body>
<?php
$a=90;
if($a%2==0)
echo “The first number is Even”;//Statement1
else
echo “The number is not even”;//Statement2
?>
</body>
</html>

Output:The number is Even(Here $a is divisible by 2 so $a%2 will be evaluated and return 0.So $a%2==0 will return true as 1 and Statement1 will be executed)

3. Program on If-else statement

<html>
<body>
<?php
$a=91;
if($a%2<>0)
echo “The number is odd”;//Statement1
else
echo “The number is not odd”;//Statement2
?>
</body>
</html>

Output: The number is odd(Here $a is not divisible by 2, so $a%2 will be evaluated and return 1.So $a%2<>0 will return true as 1 and Statement1 will be executed)

4. Program on If-else statement

<html>
<body>
<?php
$a=91;
$b=91;
if($a==$b)
echo “Two numbers are equal”;//Statement1
else
echo “Two numbers are not equal”;//Statement2
?>
</body>
</html>

Output: Two numbers are equal(Here $a and $b are containing the same value.So $a==$b will be evaluated and will return 1, and then Statement1 will be executed)

Multiple If with multiple Statements

Syntax with multiple expressions and multiple Statements:

if(expression1)
statement1;
if(expression2)
statement2;
if(expression3)
statement3;
if(expression4)
statement4;
.
.

Here firstly expression1 will be evaluated and if returns 1 then statement1 will be executed. If expression1 will be evaluated and does not return 1, then next expression expression2 will be evaluated and if returns 1 then statement2 will be executed.

If expression2 will be evaluated and does not return 1, then next expression will be evaluated. This process will be continued for others.

Program with multiple expressions and multiple statements

<html>
<body>
<?php
$phys=90;
$chem=56;
$math=99;
$comp=100;
$sum=$phys+$chem+$math+$comp;
$avg=$sum/4;
if($avg>0&&$avg<=40)
echo "Fail";
if($avg>40&&$avg<=60)
echo "Grade c";
if($avg>60&&$avg<=70)
echo "Grade b";
if($avg>70&&$avg<=80)
echo "Grade a";
if($avg>80&&$avg<=90)
echo "Grade e";
if($avg>90&&$avg<=100)
echo "Grade o";
?>
</body>
</html>

Output:Grade e(Here every if expression is containing two sub expressions. e.g $avg>90&&$avg<=100 here $avg>90 is the first expression and $avg<=100 is the second expression.

Firstly here $avg>90 will be evaluated and then $avg<=100 will be evaluated.If they return true as 1 then ($avg>90&&$avg<=100) will return true as 1)

If..elseif..elseif..else statement

It is used to execute a group of codes if any of several conditions is true.

Syntax of if..elseif..elseif..else statement

if(expression1)
statement1;
elseif(expression2)
statement2;
elseif(expression3)
statement3;
elseif(expression4)
statement4;
.
.
else
statement;

Here firstly expression1 will be evaluated and if returns 1 then statement1 will be executed. If expression1 will be evaluated and does not return 1, then next expression expression2 will be evaluated and if returns 1 then statement2 will be executed.

If expression2 will be evaluated and does not returns 1 then next expression will be evaluated and tested. This process will be continued for others.

If none of the expression is evaluated to true, then the statement will be executed.
Program with multiple expressions and multiple statements

<html>
<body>
<?php
$phys=90;
$chem=56;
$math=99;
$comp=100;
$sum=$phys+$chem+$math+$comp;
$avg=$sum/4;
if($avg>0&&$avg<=40)
echo "Fail";
elseif($avg>40&&$avg<=60)
echo "Grade c";
elseif($avg>60&&$avg<=70)
echo "Grade b";
elseif($avg>70&&$avg<=80)
echo "Grade a";
elseif($avg>80&&$avg<=90)
echo "Grade e";
else
echo "Grade o";
?>
</body>
</html>

Output:Grade e