PHP - If…Else Statement
PHP - IfElse Statement
The ability to implement conditional logic is the fundamental requirement of any programming language (PHP included). PHP has three keywords (also called aslanguage constructs) - if, elseifandelse- are used to take decision based on the different conditions.
Theifkeyword is the basic construct for the conditional execution of code fragments. More often than not, theifkeyword is used in conjunction withelsekeyword, although it is not always mandatory.
If you want to execute some code if a condition is true and another code if the sme condition is false, then use the “if…else” statement.
Syntax
The usage and syntax of theifstatement in PHP is similar to that of the C language. Here is the syntax ofifstatement in PHP −
if (expression)
code to be executed if expression is true;
else
code to be executed if expression is false;Theifstatement is always followed by a Boolean expression.
- PHP will execute the statement following the Boolean expression if it evaluates to true.
- If the Boolean expression evaluates to false, the statement is ignored.
- If the algorithm needs to execute another statement when the expression is false, it is written after theelsekeyword.
Example
Here is a simple PHP code that demonstrates the usage ofif elsestatements. There are two variables $a and $b. The code identifies which one of them is bigger.
<?php
$a=10;
$b=20;
if ($a > $b)
echo "a is bigger than b";
else
echo "a is not bigger than b";
?>When the above code is run, it displays the followingoutput−
a is not bigger than bInterchange the values of “a” and “b” and run again. Now, you will get the following output −
a is bigger than bExample
The following example will output “Have a nice weekend!” if the current day is Friday, else it will output “Have a nice day!” −
<?php
$d = date("D");
if ($d == "Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>It will produce the followingoutput−
Have a nice weekend!Using endif in PHP
PHP code is usually intermixed with HTML script. We can insert HTML code in theifpart as well as theelsepart in PHP code. PHP offers an alternative syntax forifandelsestatements. Change the opening brace to a colon (:) and the closing brace toendif;so that a HTML block can be added to theifandelsepart.
<?php
$d = date("D");
if ($d == "Fri"): ?>
<h2>Have a nice weekend!</h2>
<?php else: ?>
<h2>Have a nice day!</h2>
<?php endif ?>Make sure that the above script is in the document root of PHP server. Visit the URLhttp://localhost/hello.php. Following output should be displayed in the browser, if the current day is not a Friday −
Have a nice day!Using elseif in PHP
If you want to execute some code if one of the several conditions are true, then use theelseifstatement. Theelseiflanguage construct in PHP is a combination ofifandelse.
- Similar toelse, it specifies an alternative statement to be executed in case the originalifexpression evaluates to false.
- However, unlikeelse, it will execute that alternative expression only if theelseifconditional expression evaluates to true.
if (expr1)
code to be executed if expr1 is true;
elseif (expr2)
code to be executed if expr2 is true;
else
code to be executed if expr2 is false;Example
Let us modify the above code to display a different message on Sunday, Friday and other days.
<?php
$d = date("D");
if ($d == "Fri")
echo "<h3>Have a nice weekend!</h3>";
elseif ($d == "Sun")
echo "<h3>Have a nice Sunday!</h3>";
else
echo "<h3>Have a nice day!</h3>";
?>On a Sunday, the browser shall display the followingoutput−
Have a nice Sunday!Example
Here is another example to show the use ofif-elselif-elsestatements −
<?php
$x=13;
if ($x%2==0) {
if ($x%3==0)
echo "<h3>$x is divisible by 2 and 3</h3>";
else
echo "<h3>$x is divisible by 2 but not divisible by 3</h3>";
}
elseif ($x%3==0)
echo "<h3>$x is divisible by 3 but not divisible by 2</h3>";
else
echo "<h3>$x is not divisible by 3 and not divisible by 2</h3>";
?>The above code also usesnestedifstatements.
For the values ofxas 13, 12 and 10, theoutputwill be as follows −
13 is not divisible by 3 and not divisible by 2
12 is divisible by 2 and 3
10 is divisible by 2 but not divisible by 3