Basic Tutorial : PHP If Else Statement With Multiple Conditions

Share:

Basic Tutorial : PHP If Else Statement With Multiple Conditions. Another basic php very important and must be controlled by a web development is a php if else statement. This function is so important because it can be used as the basis of most of the making of a web programs and online tools.
Basic Tutorial : PHP If Else Statement With Multiple ConditionsPHP If else statement is essentially the same as other programming languages ​​(eg, Java, or C), the procedure is the same writing, as well as in its use. If Else Statement is used as a condition statement to check a condition, and will be working on the condition of true or false. In PHP, we have some statements the following conditions :

Basic Tutorial : PHP If Else Statement With Multiple Conditions

PHP If Statement
PHP If Statement is a php statement that is used to execute a portion of code only if the condition evaluates to true. In these conditions, nothing will be done if the value is false. Only valid if the condition is true.
Syntax :

If(condition)
{
Statement (true condition)
}

For example :

<?php
$day = date("D");
 if($day == "Sat")
 {
 echo "Happy weekend !";
 }
?>

In the example above, if the day is shown Saturday, then this php if statement will bring up the phrase “Happy weekend !”.

PHP If Else Statement
PHP If Statement is a php statement that is used to execute the two conditions in which the condition is true and the other is false.
Syntax :

If(condition)
{
Statement 1 (true condition)
}  else  {
Statement 2 (false condition)
}

For example :

<?php
$day = date("D");
 if($day == "Sat")
 {
 echo "Happy weekend!";
 } else {
echo "Good  jobs!";
}
?>

If the day is shown on Saturday then you will see the greeting, Happy weekend! on the web browser screen but other then that is display this sentence: Good jobs!.

PHP If Else … Else If Statement
PHP If Else … Else If Statement is a statement that is used to execute some of the conditions that might occur. To find out more, please study the following …
Syntax :

If(condition1){
Statement 1 (true condition 1)
}  else if  (condition2) {
Statement 2 (true condition2)
}  else if  (condition3) {
Statement 3 (true condition 3)
}  else {
Statement 4 (false condition)
}

For example :

<?php
$day = date("D");
if($day == "Mon"){
} else if ($day == "Tue"){
echo "Happy tuesday and good job !";
} else if  ($day == "wednesday"){
echo "Happy wednesday and good luck !";
} else if ($day == "Thu"){
echo "Happy thursday and nice day !"; 
} else if ($day == "Fri"){
echo "Friday prayer time !";
} else {
 echo "Happy weekend !";   
}
?>

PHP Multiple If Else Statement

Php function if else the latter is Multiple If Else Statement. This PHP statement is used to execute a number of conditions that may have had certain conditions. This function is a combination of some of the previous if else function. So, the syntax of multiple if else statements is not complicated, as long as you have known in advance about PHP If Else mainly to PHP If Else … Else If Statement syntax and usage. Here’s a simple example of the syntax of this function.
Syntax :

If(condition1){
            If (condition 1.1)
            {
            Statement 1.1.1 (true condition1.1.1)
            } else{
            Statement 1.1.2 (false condition 1.1.2)    
            }             
}  else if  (condition2) {
            If (condition 2.1){
            Statement 2.1 (condition 2.1)                 
            } else if condition 2.2) {
            Statement 2.2 (condition 2.2) 
           } else {                
            Statement 2.3 (condition 2.3) 
           }
}  else if  (condition3) {
Statement 3 (true condition 3)
}  else {
Statement 4 (false condition)
}

Multiple if else can be more complex if the condition in which there is a sub conditions in each sub-conditions resulting in multiple if else like this example.
For example :

<?php
$day = date("D");
$H = date("H");
if($day == "Mon"){
         if($H < 17)
         {
         echo "Happy monday and work hard !";
         }else{
         echo " Time for a break";
         }
} else if ($day == "Tue"){
echo "Happy tuesday and good job !";
} else if  ($day == "wednesday"){
echo "Happy wednesday and good luck !";
} else if ($day == "Thu"){
echo "Happy thursday and nice day !";   
} else if ($day == "Fri"){
         if($H < 12)
         {
         echo "Happy friday and always success !";
         } else if($H > 12 & $H < 13) {
         echo "Friday prayer time !";
         } else if($H > 13 & $H < 17) {
         echo "Back to work !";		 
         }else{
         echo "Time for a break !";
         }
} else {
 echo "Happy weekend !";   
}
?>

If we do a lot of practice using this function, the use simple and complex from if else statement will not be difficult for us. Keep learning php and good luck …
Thanks for reading Basic Tutorial : PHP If Else Statement With Multiple Conditions

[Tutorial.World.Edu]

2 thoughts on “Basic Tutorial : PHP If Else Statement With Multiple Conditions

Leave a Reply

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