note 49188 deleted from control-structures.switch by peehaa

From: Date: Mon, 02 Dec 2013 09:15:33 +0000
Subject: note 49188 deleted from control-structures.switch by peehaa
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-197702@lists.php.net to get a copy of this message
Note Submitter: rdoggett at oz dot net ---- Here's an often overlooked way of doing (nearly) the same thing: <?php echo ($i == 0) ? "i is zero" : (($i == 1) ? "i equals 1" : (($i == 2) ? "i equals 2" : "")); ?> This may be an idiomatic surprise at first. But the clean and concise code speaks for itself. Beware; PHP seems to parse the ternary operator with a different precedence than other languages such as C or perl or javascript. This means PHP requires nested parenthesis around each nested group to avoid unexpected results. Even so, this construct is still very understandable and maintainable, compared to the equivalent switch or if statements.

« previous php.notes (#197702) next »