note 74253 deleted from control-structures.alternative-syntax by mgf

From: Date: Thu, 28 Aug 2008 12:45:07 +0000
Subject: note 74253 deleted from control-structures.alternative-syntax by mgf
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-143920@lists.php.net to get a copy of this message
Note Submitter: geekman at Textbook Torrents dot com ---- The bug in your example is user error. <? $foobar = 2; echo 'Foobar is ' . ($foobar == 2) ? 'foo' : 'bar'; // outputs 'foo'; echo 'Foobar is ' . (($foobar == 2) ? 'foo' : 'bar'); // outputs 'Foobar is foo'; ?> When using the ? : operators you should always wrap the entire thing in parentheses to avoid the problem you describe. By my understanding, the issue an extension of your example: <? $pet-type = 1; $pet-name = 'Mittens'; echo 'My ' . ($pet-type == 1) ? 'cat' : 'puppy' . 'dog' . '\'s name is ' . $pet-name . '.'; // returns 'cat' echo 'My ' . (($pet-type == 1) ? 'cat' : 'puppy' . 'dog') . '\'s name is ' . $pet-name . '.'; // returns 'My cat\'s name is Mittens.' ?> You see that without parentheses the parser has no way of differentiating between the false case and the rest of the string.

« previous php.notes (#143920) next »