![]() ![]() The switch statement is used to perform different actions that based on different conditions. If we change this zero to a quoted out string and refresh the page, it will still match that zero string.PHP switch case statement was first introduced in PHP 5. Note that conditions on switch statements are loose type checks, meaning that it will also match strings of the same value. When it's zero, it will say, there are no posts. When it is three, this will say, there are few posts. ![]() Let's go ahead and save and test out this logic for number of post equal to 10. Finally, we learned how using switch-case can be faster than if-else blocks in certain situations. We also saw how to write case statements that cover multiple conditions. Let's replace this with a P-tag and we will just use a short echo tag to output the message variable. In this tutorial, I showed you how to use switch-case statements in PHP in place of if-else blocks to execute code conditionally. Finally, let's update the display to display this message variable down here rather than our ternary. We do not need a break keyword here since this is the end of the switch statement. 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. This will just be the default keyword followed by a colon. Finally, we can also supply a default fall back to handle the event when any of these case statements do not match a condition. 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. They will all fall back to this same message variable. With the use of the switch statement you can check for all these conditions at once, and the great thing is that it is actually more efficient programming to do. 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. For this match, we will set message equal to there are a few posts. Let's set up our case three to match our logic for only matching three posts. For any other condition matches, we will just repeat this process. Since we don't want the logic in this block to continue, we will add a break statement. Let's create a new message variable and we will set it equal to there are no posts. Now everything after this colon is considered a separate logic block, and all code continues to execute until it encounters a break keyword. For example, if we wanted to target zero post, we will write out case zero. Since this will be an integer, the matching result will be a number. Next comes a case keyword followed by the result we wish to match to this condition of num posts. The following format is where things start to differ. Then we will have an open and close bracket just like if-statements. A switch case statement in PHP executes different blocks of code based on a variable or an expression. Just like an if-statement, we will supply a condition within parentheses which will just be num posts. Let's go ahead and create a switch statement under this num post display variable. With a switch, you supply a condition and then define any number of cases the condition may match. 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. ![]() If-statements are OK for these situations, but they can also lead to some repeated code throughout your logic. Ternaries are only really good for simple Booleans like this or that logic, but not this, then, and then something else logic. What if we wanted to do something a bit more complex. Instructor: Right now, we are using a ternary to check if we have post and displaying an appropriate message.
0 Comments
Leave a Reply. |