1. 17
    Write multiple conditions to execute alternate PHP code
    3m 24s

Write multiple conditions to execute alternate PHP code

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 2 years ago

You'll eventually need to run a block of PHP code that executes if your "if" statement is false. In this event, you will use an "else"| clause.

Instructor: [0:00] We can also supply an else condition, which will execute an alternate block of code whenever the checked condition is false. [0:06] We can do this by adding else after our closing bracket, with another open and closed bracket. Within here, let's echo out, "There are no posts."

[0:20] To trigger this condition, the has_post must be false, so let's change number of post to zero. When we save and refresh this page, we will see our "There are no posts" message. We can also use elseif, if we have multiple conditions to check.

[0:36] Say, for example, we wanted to execute a bit of code, but only if the number of posts is exactly three. We can go ahead and add another else statement with an open and close bracket, but change this to elseif.

[0:51] We can pass in the condition for this elseif to match. Let's say num_post=3. Then let's echo out, "There are exactly three posts." If we update number of post to 3 and save and refresh the page, we will see we still get this "Posts exist" message.

[1:16] This is because PHP evaluates code from top down. This means that the first condition which matches a truthy result will be returned. Since has_post is true, this echo statement will output first, and then bypass the remaining statements.

[1:33] In order for our code to execute as we would expect, this number of post=3 check would need to come before has_posts.

[1:43] Let's switch around these conditions by moving the conditions around, and we will also change the echo statements so they appear in their appropriate places. Now if we save and refresh, we will see our "There are exactly three posts" message.

[2:03] If we set num_post back to 10 and refresh, we will see that it correctly falls back to this second condition.

[2:12] Before we move on, a quick note about this double equal sign. This is a comparison operator, but this is considered a weak check. This is because if num_posts actually equals a string, let's say a string of "three," this will still evaluate to true.

[2:31] A double equal sign will only check the results of these values, and not the actual data types of the values. In order to enforce a strict check of not only the values but also the data types, we will use a triple equal sign.

[2:48] This will check not only the value of each side of this equation, but also make sure the data types match.

[2:54] If we save and refresh the page here, we will see that this condition evaluates to false since "three" is not an integer. If we change this to an integer, this will return true.

[3:07] A triple equal sign is definitely the preferred method in PHP for comparing two values. You should really never use a double equal sign unless you had a very specific reason to do so, and it's probably not a good one.

~ a year ago
<?php if ($hasPost ===3) echo 'Post equal to 3 Exist.'; elseif($hasPost) echo 'Post exist greater than 3'; else echo 'No post exist'; ?>

is there a syntax update this one seem to work for me

Mark Shust
Mark Shustinstructor
~ a year ago

This is shorthand syntax and is still valid PHP code. I recommend the bracketed syntax though as it has defined start and end points for each block of code, which makes the code more readable and easier to understand. I’d use shorthand syntax very sparingly, if at all.

~ a year ago

Thanks for sharing @Mark

Markdown supported.
Become a member to join the discussionEnroll Today