switch
The
In the following example, each code block is equivalent.
One uses a series of Example #1 The above example will output: i equals 1 i equals 1
It is important to understand how the The above example will output: i equals 1 i equals 2
Here, if $i is equal to 0, PHP would execute all of the echo
statements! If $i is equal to 1, PHP would execute the last two
echo statements. You would get the expected behavior ('i equals 2'
would be displayed) only if $i is equal to 2. Thus,
it is important not to forget
In a The statement list for a case can also be empty, which simply passes control into the statement list for the next case. The above example will output: i is less than 3 but not negative
A special case is the The above example will output: i is not equal to 0, 1 or 2
If no A case value may be given as an expression. However, that expression will be evaluated on its own and then loosely compared with the switch value. That means it cannot be used for complex evaluations of the switch value. For example: The above example will output: B
For more complex comparisons, the value The above example will output: B The alternative syntax for control structures is supported with switches. For more information, see Alternative syntax for control structures. The above example will output: i equals 1 It's possible to use a semicolon instead of a colon after a case. As of PHP 8.5.0, this syntax is deprecated. The above example will output: Deprecated: Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead in script on line 6 Deprecated: Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead in script on line 7 Deprecated: Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead in script on line 8 Deprecated: Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead in script on line 11 Please make a new selection... Warning
This feature has been DEPRECATED as of PHP 8.5.0. Relying on this feature is highly discouraged. Changelog
|