PHP Decision Making

PHP decision making or conditional statements are the statements which are used when we want to check for specific condition and based on that we want to decide which part of our PHP script is to be executed. Here to check the conditions mostly we will use relational operators.

PHP provides some kind of conditional statements used for decision making as below.

Simple IF Statement

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

Syntax

if(condition)
{
    Set of statements to be executed when condition is true
}

For Ex

<?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.

IF..ELSE Statements

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

if(condition)
{
      Set of statements to be executed when condition is true
}
else
{
      Set of statements to be executed when condition is false
}

For Ex.

<?php
	$num=10;
	if($num%2==0)
	{
		echo “You entered even number”;	
	}
	else
	{
		echo “you entered odd number”;
	}
?>

Here in our example variable $num will be checked for condition $num%2==0 if its true its even number else its odd. In short when you need to choose between two set of statements simple IF..ELSE is used.

IF..ELSE IF Ladder

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.

Syntax

if(condition1)
{
    Set of statements to be executed when condition1 is true
}
else if(condition2)
{
    Set of statements to be executed when condition2 is true
}
..
..
else if(conditionN)
{
    Set of statements to be executed when conditionN is true
}
else
{
    Set of statements to be executed when no condition is true
}

For Ex.

<?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. Note that else is a default case which is executed when none of the conditions in IF and ELSE IF is not satisfied.

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.

Syntax

if(condition1)
{
     if(condition2)
     {
         Statements when condition 2 is true
     }
     else
     {
         Statements when condition2 is false
     }
}
else
{
    Statements when condition1 is false
}

Note that Nested If statements can be written for any IF..ELSE ladder its one alternative way to write multiple conditions.

For Ex.

<?php
	$a=10;
	$b=9;
	$c=11;
	if($a>$b)
	{
		if($a>$c)
		{
			echo $a.” is greatest number”;
		}
		else
		{
			echo $c. ” is greatest number”;
		}
	}
	else
	{
		if($b>$c)
		{
			echo $b.” is greatest number”;
		}
		else
		{
                        echo  $c.” is greatest number”;
		}
	}
?>

The example given above demonstrates the use of nested if statements for finding the greatest number among given three variables $a , $b & $c.

So to conclude in this tutorial we have learnt decision making statements like simple if, if else statements, if else if statements and nested if statements.  It’s used when we want to choose from specific set of statements on basis of conditions.

Share this Article on Social Media

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments