PHP If Else Statement

PHP-If-Statement-Programming-Pot
••• PHP If Else Statement

After reading about the basic concept of PHP Control Structure In this tutorial, you will learn to control flow control of a program using PHP If ELse Statement. Normally, statements in a program are executed one after the other in the order in which they are written. This is called sequential execution.

But sometimes, along with the program, the order of execution needs to be changed or the programmer must know how to change the flow of the program.

For this PHP provides control statement if and if..else to control the flow of program.

 

PHP If Else Statement


Most basic form of decision making is PHP If Statement in which some statements are processed only if specified condition is satisfied otherwise those statements will not be executed.

It’s a one-way branching in which the statements will only execute if the given expression is true.

Syntax of PHP If Else Statement

if (condition)
 {
   true statements;
 }

Here, statements will be executed only if the expression is true.

#For Example:

<?php
	$age = 21;
	If($age >= 18)
	{
	   echo “You are eligible for giving VOTE”;
	}
?>

Here in our example $age is checked against condition $age >= 18 if condition is satisfied the message inside the if block will be executed otherwise not.

PHP If else Statement


if..else statement is used if we want to execute some code if the condition is true and another code if the condition is false.

This kind of conditional statements are used when we want a process to be performed when the condition is true, when false we want to process another set of statements. Thus when we want both true and false conditions we use if..else statement.

Syntax of if..else Statement

Here, if an expression is true then statement1 will be executed , otherwise,  statement2 will be executed.

<?php
    if (expression)
     {
      statement1;
     }
    else
     {
     statement2;
     }
?>

Here, if the expression is true then statement1 will be executed otherwise, statement2 will be executed.

#For Example

 if grades are less than 40 you will get failed result, otherwise you will get pass result

<?php
      $grade = 60;
      if($grade >= 40)
	{
	  echo “Congratulations You are Passed.”; 
     } else 
         { 
        echo “Sorry You are Failed.”; 
      } 
?>
// Output
Congratulations You are Passed.

PHP if..else if…else Statement


if...else if...else statement is used to select a particular block of code when several block of codes are there.

When we want to check for specific set of conditions and on basis of that we want to decide which statements to be executed if..else if ladder is used. In short if..else if is advanced version of simple if..else statements which allows us to check for multiple conditions. Ladder means staircase. If more conditions are to be checked then this type of statement is used.

Structure of if..else if.. else Statement

<?php
	if(expression1)
 	 {
 	   statement1;
 	 }
 	else if (expression2)
 	 {
 	   statement2;
 	 }
 	else if (expression3)
 	 {
 	   statement3;
 	 }
?>

Here, statement1 will be executed if expression1 is TRUE, statement2 will be executed if expression2 is TRUE and if none of the expressions are true then statement3 will be executed.

Note: Syntax error is detected by the compiler whereas logic error has its effect at execution time.

#For Example:

<?php
	$num=10;
	if($num>0)
	{	
	       echo “$num is Positive Number”;
        }
        else if ($num<0)
        {
	       echo “$num is Negative Number”;
        }
        else
        {
	      echo “$num is Zero”;
        }
?>

In our example we are trying to decide whether a number entered is positive, negative or zero for that we have used if..else if Ladder.

PHP Nested If Statements


Nested Means one inside another, When we want to check if the first condition is true , and again for the same if we want to check another condition in such case we will use Nested IF Statements. It is called nested if because inside one IF we have other IF statement to be checked.