PHP Decision Making Statements

We saw in the previous lesson, how to work with PHP loops, while, do..while, for, and for…each. Now, we learn PHP statements with examples and illustrations. With PHP decision-making statements, you can easily take decisions based on different conditions.

Let’s discuss the decision-making statements,

  • if statement
  • if…else statement
  • if…else if…else statement
  • switch statement

if statement

The if decision-making statement executes the code, if the condition is true.

Here’s the syntax,


Here, the code executes if the condition $a > 5 is true. The value of a is greater than 5, so the if statement executes and prints, We’re learning PHP,

Here’s the output,

PHP decision making statements - if statement

if…else statement

The if…else decision-making statement executes some code if the condition is true and another code if the condition is false. The false condition comes under else.

Here’s the syntax,


The value of $a = 10, so the first statement won’t execute, since its condition is $a < 5.
However, the second statement else executes and prints We’re learning Python as shown below,

Here’s the output,

PHP if else decision making statement

if…else if…else Statement

The if…else if…else statement executes code for different conditions. If more than 2 conditions are true, you can use else for the false condition.

Here’s the syntax,


Here, $a = 5, so the else condition gets executed.

Here’s the output,

PHP elseif decision making statement

PHP Switch Statement

If you want to avoid using long code for if…elseif…else, then use the switch statement. Under switch, you can add many blocks of code and select one at a time.

Here’s the syntax,


Here, the Java topic gets printed as output.

Here’s the output,

PHP switch decision making statement

Here, we saw how to work with PHP Decision-making statements, if, if..else, if…else…elseif, switch, etc.

PHP Loops
PHP String Handling
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading