PHP if…else…if statement

PHP if…else…if statement

if…elseif….else statement – use this statement to select one of several blocks of code to be executed.

The if…elseif….else statement

This statement is used to select one of several blocks of code to be executed.

The if..else statement executes if a specified condition is true or false. It means in if..else we can mention one to two conditions. But in if..elseif..else,we can select number of blocks to be executed.

Syntax:

if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true; else
code to be executed if condition is false;

To understand better the if…elseif….else statement, let’s try some example:

<?php 
$name= ”Piolo”; 
if ($name == ”John Lloyd”) 
{ 
echo "Sorry, you’re not Piolo"; 
} 
elseif($name == ”Sam”) 
{ 
echo "Sorry, you’re not Piolo"; 
} 
else 
{ 
echo "Sorry, you’re not Piolo"; 
} 
?>

How it works:
  • In our example we assigned Piolo into our variable $name.
  • The first condition is if the name is equal to John Lloyd then it will output Sorry, you’re not Piolo.
  • The second condition is if the name is equal to Sam then it will output Sorry, you’re not Piolo.
  • Since the first and second condition is false then the output will be Sorry, you’re not Piolo.
, , , , , , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.