Switch in PHP
Switch statements express conditionals across many branches.
Here’s a basic switch
in PHP.
You can use commas to separate multiple expressions in the same case
statement. We use the optional default
case in this example as well.
switch
without an expression is an alternate way to express if/else logic. Here we also show how the case
expressions can be non-constants.
A type switch
in PHP is done by checking the type using functions like is_bool
, is_int
, etc. This is how we can discover the type of a variable.
To run the translations above, save each code snippet into a .php
file and run them using php
from the command line.
Next example: Arrays.