note 103597 modified in language.operators.comparison by danbrown
| From: | danbrown@php.net | Date: | Thu, 21 Apr 2011 18:20:08 +0000 |
| Subject: | note 103597 modified in language.operators.comparison by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-179060@lists.php.net to get a copy of this message | ||
be careful when trying to concatenate the result of a ternary operator to a string
<?php
print '<div>'.(FALSE) ? 'TRUE [bad ternary]' : 'FALSE [bad
ternary]';
print '<br><br>';
print '<div>'.((FALSE) ? 'TRUE [good ternary]' : 'FALSE [good
ternary]');
?>
yields:
TRUE [bad ternary]
FALSE [good ternary]
this is because the ternary evaluates '<div>'.(FALSE) not (FALSE) - so the end
result is TRUE
--was--
be careful when trying to concatenate the result of a ternary operator to a string
print '<div>'.(FALSE) ? 'TRUE [bad ternary]' : 'FALSE [bad
ternary]';
print '<br><br>';
print '<div>'.((FALSE) ? 'TRUE [good ternary]' : 'FALSE [good
ternary]');
yields:
TRUE [bad ternary]
FALSE [good ternary]
this is because the ternary evaluates '<div>'.(FALSE) not (FALSE) - so the end
result is TRUE
http://php.net/manual/en/language.operators.comparison.php