note 12136 deleted from control-structures.switch by spic
| From: | spic@php.net | Date: | Wed, 28 Aug 2002 08:48:37 +0000 |
| Subject: | note 12136 deleted from control-structures.switch by spic | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-35882@lists.php.net to get a copy of this message | ||
The switch statement performs an equality comparison (like using ==). Therefore, the value specified
at the beginning of the switch statement can be any valid expression (it's comparable to the
left hand operand of a == comparison), and the value specified for each case may be any valid
expression (comparable to the right hand side of a == comparison).
To illustrate:
Here's a typical equality comparison with if:
if (left_hand_operand == right_hand_operand) ...
This is comparable to a switch like this:
switch (left_hand_operand)
{
case right_hand_operand: ...
}
As you can see, switch implies a comparison using ==.
Logically, it follows that arrays and objects also work in switch statements. I don't recommend
it (the contents of an object, for example, aren't as predictable as those of a variable of
simple type [numeric or string]), but using a complex type (array or object) does, indeed, work.