PHP Switch Statement
Switch Statement is the same as If…Else Statements except that it is easier to use if you have multiple conditions.
- switch (condition)
- case condition1:
- statement to be executed if n=condition1;
- case condition 2:
- statement to be executed if n=condition2;
- statement to be executed if n is different from both condition 1 and condition 2;
condition is a variable expression that is evaluated once. If the first condition is true then the succeeding condition will not be executed anymore, else it will continue until it finds the condition that is equals to true or execute the default statement if no matching condition.
- switch ($lastname)
- case “smith”:
- echo "Welcome Mr. Smith";
- case “cruz”:
- echo "Welcome Mr. Cruz";
- echo "Welcome Guest";