1. 21
    Use a match statement for advanced PHP data checks
    3m 7s

Use a match statement for advanced PHP data checks

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

A switch statement includes a few awkward caveats that don't exist in PHP 8's new match statement. In this lesson, we will learn how to use a match statement to execute when a condition matches a specific value.

Instructor: [0:00] A new feature introduced in PHP 8 is the match statement, which is an alternative to the switch statement. [0:06] Some complaints about switch statements had been that it's a bit too verbose, you can forget to include one of these break keywords and your logic will get messed up, and that the conditional check is loose rather than strict.

[0:21] The match statement aims to solve all of these issues.

[0:24] Let's rewrite this entire switch statement into a match statement. Our first change will be that we will assign the match statement to a variable instead, and that will be this message variable. This implies right away that a value is returned from match. Let's go ahead and set it equal to match, and the condition will be still number of posts.

[0:51] Next comes the values that we wish to match. We'll type zero here for matching zero posts, and then assign this the value of "There are no posts."

[1:03] Note how we are assigning variables here. It's an equal sign, followed by a greater than sign. This is how key value assignments are made in PHP. The key is the zero, and the value is this "There are no posts" string.

[1:19] Since this is a variable assignment, we also need to make sure to end the match statement with a semicolon.

[1:26] With key value assignments, it's common to add more assignments in later, so it's a good idea also to end every single one of these lines with a comma. If you use version control, making updates to these key value assignments will lead to cleaner diffs in your code.

[1:42] Let's create another possible match, and this time for one. Let's add on a two and three as well. This is a huge spot where match differs from switch. Rather than typing case every time, we could just type out the numbers delimited with commas. Then we can return, "There are some posts."

[2:03] You may have noticed something else here as well. There are no break statements with match. As soon as a condition is matched, it returns the value and exits out of the match statement. This can lead to code that is a bit easier to understand, and you never need to worry about forgetting the break keyword.

[2:21] Finally, just like switch, we can define a default fallback. For this value, we can just return, "There are many posts."

[2:32] Just like that, we have the same logic as our switch case statement. If we test this out, we will see that this is true. In addition, note that this is a strict data check, which means that if we pass a string, it will not catch the resulting matches with a different data type.

[2:50] For example, if we pass in a 3 and save and refresh, we will get our "There are some posts" message. If we pass in a string of "three," we will get, "There are many posts," because it will fall back to this default condition.

egghead
egghead
~ a minute 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