Contact Information

KodeBlog

We're Available 24/ 7. Drop us an email.

PHP - Decision Making

PHP Decision making works by using three set of statements respectively depending on the different scenarios. These statements are called conditional statements and they can be used in the code for the purpose of decision making.

Topics to be covered

We will cover the following topics in this lesson

  • If...Else Statement
  • Elseif Statement
  • Switch Statement
  • Summary

If...Else Statement

We will use this conditional statement if we want to execute a set of code where one condition is true and another is false.

if (condition)
	code for the TRUE condition will be written here;
else
	code for the FALSE condition will be written here;
end if

 

For Example:

<?php

$gender = "Female";

if ($gender == "Male"){
    echo "He is a Guy";
}
else{
    echo "She is a Girl";
}

?>

HERE,

  • $gender = "Female"; assigns the value Female to the variable $gender.
  • if ($gender == "Male"){...} checks to see if the value of the variable $gender is equal to Male then outputs He is a Guy
  • else{...} the keyword else is used to specify the block of code to be executed if the condition is false. In this case, we out the words She is a Girl.

The above code outputs the following

She is a Girl

Elseif Statement

Elseif will be use when there are more than 1 conditions that are true and only 1 condition is false.

if (condition)
	code to be executed when the first condition is true;
elseif (condition)
	code to be executed if the second condition is true
else
	code to be executed when the condition is false;
end if

For Example

<?php

$day = date("D");

if ($day == "Sat"){
    echo "It's Saturday night fever!";
}
elseif ($day == "Sun"){
    echo "It's Sunday Happy day off";
}
else{
    echo "Still a working day";
}

?>

HERE,

  • $day = date("D"); assigns the first three (3) letters of the day to the variable $day
  • if ($day == "Sat"){...} checks to see if the value of $day is Sat
  • elseif ($day == "Sun"){...} checks to see if the value of $day is Sun
  • else {...} defines the block to be executed if the first and second conditions are false.

Assuming you run the code on Saturday, you should be able to get the following output

It's Saturday night fever!

 

In the above example Saturday and Sunday are classified as holidays whereas remaining days are classified as working day. You can use the example code in the way you want to fit for your own customized needs and requirement whether it's about Date, Class room information, School Data, Office data, Restaurant or Airline data etc..

Switch Statement

Switch Statement is used when the condition of true and false exceeds more than normal or are too many. Switch statement is ideal to use for scenarios like MCQ's and whenever the user has to chose from multiple options.

switch (expression)
{
    case option1:
    	code to be executed if expression = option1;
    	break;

    case option2:
    	code to be executed if expression = option2;
    	break;

    case option3:
    	code to be executed if expression = option3;
    	break;

    default:
    	code to be executed if expression is different from option1,2 and 3;
}

 

For Example

<?php

$day = date("D");

switch ($day)
{
    case "Mon":
        echo "It's a Monday";
        break;
    case "Tue":
        echo "It's a Tuesday";
        break;
    case "Wed":
        echo "It's a Wednesday";
        break;
    case "Thu":
        echo "It's a Thursday";
        break;
    case "Fri":
        echo "It's a Friday";
        break;
    default:
        echo "It's the Day Off";
}

?>

HERE,

  • $day = date("D"); assigns the first three letters of the day to the variable $day.
  • switch ($day) the switch statement evaluates the value of the variable $day which it then compares against the various case statements within the code block.
  • case "Mon": checks to see if the value returned is Mon then outputs It's a Monday. The break statement is used to step out of the loop. The same logic applies to the values Tue, Wed, Thu, and Fri.
  • default: defines the block to be executed if the above cases have all evaluated to false. In our case, the default will respond to Sat and Sun.

As you can see for all the working days the code will output its name except for both Saturday and Sunday it will output as a day off.

Summary

Decision making in PHP allows us to choose the program execution path depending on the evaluation of a predefined consition. For example, we can use the if statement to check if the username and password are valid. If yes, then we grant access to the system, else we deny access. The switch statement is usually used to evaluate multiple conditions. If you have multiple else if statements then you should probably use the switch statement instead.

SUPPORT US

If you found this tutorial useful, support us by using the social media buttons to like and share the tutorial. If you didn’t find it useful, please use the comments section below to let us know how we can do better next time.

Subscribe to our newsletter, like our Facebook fan page or follow us on Twitter to get free updates when we publish new tutorials

Author: Rodrick Kazembe

Rodrick is a developer who works on Desktop, Web and Mobile Applications. He is familiar with Python, Java, JavaScript, C++, C#, Kotlin, PHP, Python and the list goes on. Rodrick enjoys sharing knowledge especially when it comes to technology.

PHP 7 New Operators

PHP - Loop Types

Leave A Reply