May 10, 2021 PHP
Sometimes, to avoid if statements being too lengthy and to improve the readability of your program, you can use switch branches to control statements.
Switch statements are used to perform different actions based on several different conditions.
Use the switch statement if you want to selectively execute one of several blocks of code.
How it works: Start by evaluating a simple expression n (usually a variable). C ompare the value of the expression with the value of each case in the structure. I f there is a match, the code associated with the case is executed. A fter the code is executed, use break to prevent the code from jumping into the next case to continue execution. The default statement is used to execute when there is no match (that is, no case is true).
Tip: Don't miss break when using switch statements, as this may surprise your output.