Ifs, Else Ifs, Switch

Updated onbyAlan Morel
Ifs, Else Ifs, Switch


Logic in any programming language is the ability for your program to make decisions and choose a path to take depending on the state. For example, if you're writing a calculator, you're going to have to perform different operations depending on what button the user presses.

In this lesson, we'll learn all the ways to express logic in our PHP code by first learning about the comparison operators that make it possible.

Equality Operator

Using the equality operator, we can check if two numbers or "things" are equal to one another or not.

If the things we are comparing are equal, using the equality operator on them will result in true, and if they are not equal, it will result in false.

Using this operator is straightforward, simply use three equal signs.

<?php echo(7 === 7); $peanuts = 48 / 4; echo($peanuts === 12); echo(63 === 42); ?>
<?php var_dump(7 === 7); $peanuts = 48 / 4; var_dump($peanuts === 12); var_dump(63 === 42); ?>
bool(true) bool(true) bool(false)

Inequality Operator

The inequality operator works in a similar fashion but in the reverse. If what you're comparing is the same, the result will be false, otherwise it will return true.

To use this operator, use a single exclamation point followed by two equal signs.

<?php var_dump(9 !== 9); var_dump(57 !== 26); ?>
bool(false) bool(true)

Greater Than and Less Than Operators

You can check if a numerical value is greater than or less than another one using the greater than and less than operators.

<?php var_dump(4 < 3); var_dump(34 > 19); $variable = 62; var_dump($variable > 25); var_dump($variable > 131); ?>
bool(false) bool(true) bool(true) bool(false)

Or Equal To Operators

In the case you need to check if a number is either equal to or greater/less than another number, there are operators specifically for that as well.

To check if a number is greater than or equal to another number, use a greater than symbol followed by equal sign. On the flip side, to check if a number is less than or equal to another number, use a less than symbol followed by an equal sign.

<?php var_dump(34 <= 61); var_dump(43 <= 20); var_dump(76 >= 76); var_dump(83 >= 92); ?>
bool(true) bool(false) bool(true) bool(false)


Now that we are able to compare things together and get back a boolean as a result, we are ready to make our program make decisions based off of that result. When we made decisions based off of the results of something, that is called a conditional.


The if statement is very straightforward. If whatever condition you are checking returns true, then the next block of code will execute.

Let's say if you eat 4 or more donuts, you will become sleepy.

<?php $donuts = 5; if ($donuts >= 4) { echo('I am sleepy!'); } ?>
I am sleepy!

Clearly you could care less about being sleepy, because you ate 5 whole donuts. But what if you had some self-control and only ate 3 donuts?

<?php $donuts = 3; if ($donuts >= 4) { echo('I am sleepy!'); } ?>

Nothing was printed because the code block did not run. The reason it did not run was because the conditional returned false, as 3 is not greater than or equal to 4.


When you want to run a code block if the original conditional returns false, you can utilize the else keyword.

Let's add an else block to our original situation.

<?php $donuts = 3; if ($donuts >= 4) { echo('I am sleepy!'); } else { echo('I can eat another donut!'); } ?>
I can eat another donut!

Because this time we added the else code block, when the $donuts <= 4 conditional returned false, it skipped the first code block and executed the second code block.

Else If

The else if keywords are a combination of both an if and an else block. You use these in between an if and an else statement and it serves as an additional path your code can take.

<?php $donuts = 2; if ($donuts >= 4) { echo('I am sleepy!'); } else if ($donuts === 3) { echo('I can eat another donut!'); } else if ($donuts === 2) { echo('I really want another donut!'); } else { echo('I NEED donuts'); } ?>
I really want another donut!

First it compares 2 to 4, that's false so it moves on. Now it compares 2 to 3, that's also false so it continues on. Now it compares 2 with 2, and since that's true, the code executes. The final else is never called and the entire program moves on.

Ternary Operator

The ternary operator is the shorthand way to write an entire conditional on a single line. Use ternary operators to assign a variable a value depending on the outcome of the conditional.

<?php $donuts = 4; $sleepy = ($donuts >= 4) ? 'Yes' : 'No'; echo('Am I sleepy? ' . $sleepy); ?>
Am I sleepy? Yes

If you eat 4 or more donuts, the value of $sleepy will be Yes, otherwise it will be No.

Switch Statement

The switch statement offers us an alternative way to express a situation where you might have multiple else-ifs. A switch statement is similar to a traditionally else-if as you will see in the below example:

<?php $weather = 'cloudy'; switch ($weather) { case 'rainy': echo('I do not like this weather.'); break; case 'cloudy': echo('This weather is okay.'); break; case 'sunny': echo('This is great weather!'); break; default: echo('This is a new kind of weather!'); } ?>
This weather is okay.

In our switch statement, we are switching on the $weather variable. If the value in it happens to match any of the cases we listed, the corresponding code block will execute. That is why it skipped over rainy and executed the code block under cloudy, as that was the value of $weather.

The break statement is there to end the switch statement from continuing on to more cases. If none of the cases we defined match, we can add a default case so that something executes, no matter what.

Logical Operators

The conditions we have been evaluating so far have been pretty simple. However, there are cases where we'll want to evaluate multiple conditions at once before making a decision.

There are many ways to combine and evaluate more than one condition at once, and we'll look at the logical operators that make that possible.

And (&&)

The and operator evaluates multiple conditions and only returns true if both conditions return true.

<?php $lunch_time = true; $is_hungry = true; if ($lunch_time && $is_hungry) { echo('Since it is lunch time AND I am hungry, I should eat food now.'); } ?>
Since it is lunch time AND I am hungry, I should eat food now.

Since both conditions were true, the echo inside was executed.

Or (||)

When you want the entire condition to be true if either condition is true, then what you want to use is the or logical operator.

The or operator only requires a single condition to be true for the entire condition to be true:

<?php $pizza_available = true; $burger_available = false; if ($pizza_available || $burger_available) { echo('There is something to eat!'); } ?>
There is something to eat!

Even though there was no burgers available, because there was pizza, the condition returned true and you were able to eat something.

Not (!)

The not operator negates the outcome of whatever condition it is placed in front of. If the original condition returned false, then adding a not operator in front would make it act as if it returned true, and vice-versa.

<?php $weather = 'sunny'; $is_raining = $weather === 'rain'; if (!$is_raining) { echo('Since it is not raining, I can play basketball outside!'); } ?>
Since it is not raining, I can play basketball outside!

You cannot play basketball outside in the rain, so first you check if it's raining via the $is_raining variable. Since the weather is sunny and therefore not raining, you are able to play! ๐Ÿ€๐Ÿ€๐Ÿ€

Next Lesson ยป
Copyright ยฉ 2017 - 2024 Sabe.io. All rights reserved. Made with โค in NY.