1. 20
    Use a PHP switch statement for advanced conditional logic
    4m 12s

Use a PHP switch statement for advanced conditional logic

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

When you find yourself repeating or writing similar conditional logic in your if statements, a better option may be a switch statement.

Instructor: [0:00] Right now, we are using a ternary to check if we have post and displaying an appropriate message. What if we wanted to do something a bit more complex.

[0:09] Ternaries are only really good for simple Booleans like this or that logic, but not this, then, and then something else logic. If-statements are OK for these situations, but they can also lead to some repeated code throughout your logic.

[0:25] Since we want to add some additional functionality to the display of this message, in this situation it's better to use a switch statement. With a switch, you supply a condition and then define any number of cases the condition may match.

[0:40] Let's go ahead and create a switch statement under this num post display variable. This will just be switch. Just like an if-statement, we will supply a condition within parentheses which will just be num posts.

[0:55] Then we will have an open and close bracket just like if-statements. The following format is where things start to differ.

[1:02] Next comes a case keyword followed by the result we wish to match to this condition of num posts. Since this will be an integer, the matching result will be a number. For example, if we wanted to target zero post, we will write out case zero. Then next comes a colon.

[1:24] Now everything after this colon is considered a separate logic block, and all code continues to execute until it encounters a break keyword. Let's create a new message variable and we will set it equal to there are no posts.

[1:43] Since we don't want the logic in this block to continue, we will add a break statement. For any other condition matches, we will just repeat this process. Let's set up our case three to match our logic for only matching three posts. For this match, we will set message equal to there are a few posts.

[2:09] Then we will type our break keyword. One neat thing we can also do with a case statement is that if we wanted this message to also display for a result matching not only three, but also one and two, we can define multiple case statements to match it with.

[2:28] We can type case one and also case two. They will all fall back to this same message variable. Since PHP doesn't encounter a break keyword on these other case statements, it will continue processing all of these matching conditions as a single block to execute.

[2:48] Finally, we can also supply a default fall back to handle the event when any of these case statements do not match a condition. This will just be the default keyword followed by a colon.

[3:01] Since this will only execute when num post is greater than or equal to four, we will just set this message equal to there are many posts. We do not need a break keyword here since this is the end of the switch statement.

[3:19] Finally, let's update the display to display this message variable down here rather than our ternary. Let's replace this with a P-tag and we will just use a short echo tag to output the message variable. Let's go ahead and save and test out this logic for number of post equal to 10.

[3:42] This will say, there are many posts. When it is three, this will say, there are few posts. When it's zero, it will say, there are no posts. Note that conditions on switch statements are loose type checks, meaning that it will also match strings of the same value.

[4:03] If we change this zero to a quoted out string and refresh the page, it will still match that zero string.

egghead
egghead
~ 31 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today