PHP IF, Else,Elseif and Switch Statement

php if else and switch statementPHP If ,Else,Elseif and Switch statements are most important for checking condition of an expression  in PHP code.

If we need to check any condition then we can use if ,else or elseif and switch statement.Casue these are easy to use.

PHP If ,Else,Elseif and swich statement  can be used in following way:

1. if statement:

Use it when  you need to check a single condition statement is true .

2. if…elseif..else statement:

Use it when  you will need to check multiple condition statement.

3. if…else statement:

Use it when  you need to check  two  condition statement.

4. Switch Statement:

Use it when  you will need to check or select one of many blocks of code to be executed.

1. If Statement

Use it when  you need to check a specific condition is true.

Syntax:

if ( condition)

code to be executed here if condition is true;

 

Example:

<html>

<body>

<?php

$day=date("D");

if ($day=="Sun")

echo "Have a nice day!";
else
echo "Today is not Sunday";

?>

</body>

</html>

2. if…elseif..else statement:

The above example will give output Have a nice day!  if the current day is Sunday.

Use it when  you will need to check multiple condition statement.

Syntax :

if ( condition )

  {

First statement executed if condition is true;

}

elseif ( condition )

  {

Second statement executed if condition is true;

}

else

  {

Final statement executed if condition is false;

}

For Example:

<html>

<body>
<?php 
 $day=date("D");   

 if ($day=="Sun") 

{    
 echo "I will go to Silicon villi ";  
 }  
 else if ($day=="Mun")   

{    
 echo "I will go to Barbuda Triangle "; 
}   

else   
 { 
 echo "I will go to Home ";     
}
?>
</body>

</html>

3. if…else statement:

Use it when  you need to check  two  condition statement.

Syntax:

if ( condition )

  {

A Statement to be executed if condition is true;

}

else

  {

  A Statement to be executed if condition is false;

}

 For Example:

<html><body><?php$day=date("D");if ($day =="Sat") 

  {
  echo "I will buy Iphone5"; 

 } 

 else 

  { 

    echo "I will not buy Iphone5";

 } 

?>

</body>

</html>

4. php switch statement:

Use it when  you will need to check or select one of many blocks of code to be executed.

Syntax:

switch ( n )

{

case constant_1:

Statement_1 is executed if n=constant_1;

break;

case constant_2:

Statement_2 is executed if n=constant_2;

break;

default:

other statement is executed if n is different from both constant_1 and constant_2;

}

Form above statement  we can see that a Switch Statement consist of Four Keyword:

1. switch

2.case

3.break

4.default

Example of Switch statement:

<html>
<body>
<?php
$x=80/10;
switch ($x)
{case 8:

echo "Your grade is A+ ";break;

case 7:

echo "your grade is A ";break;

case 6:

echo "your grade is A-";

break;

default:

echo "your grade is F ";

}

?>

></body>

</html>

Related Post or You may like:

 

Incoming search terms:

    if elseif php multiple student grades, php if else break, Study of if if-else if-else-if and switch in PHP
Follow us on Facebookschliessen
Powered by LikeJS
oeffnen