sean Fri Sep 1 15:33:53 2006 UTC
Modified files: /phpdoc/en/language operators.xml Log: note about non-obvious ternary evaluation (and recommend to avoid stacking); bug #38679 http://cvs.php.net/viewvc.cgi/phpdoc/en/language/operators.xml?r1=1.102&r2=1.103&diff_format=u Index: phpdoc/en/language/operators.xml diff -u phpdoc/en/language/operators.xml:1.102 phpdoc/en/language/operators.xml:1.103 --- phpdoc/en/language/operators.xml:1.102 Tue May 9 18:18:34 2006 +++ phpdoc/en/language/operators.xml Fri Sep 1 15:33:53 2006 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.102 $ --> +<!-- $Revision: 1.103 $ --> <chapter id="language.operators"> <title>Operators</title> <simpara> @@ -686,6 +686,34 @@ issued in later PHP versions. </simpara> </note> + <note> + <para> + Is is recomended that you avoid "stacking" ternary expressions. PHP's + behaviour when using more than one ternary operator within a single + statement is non-obvious: + <example> + <title>Non-obvious Ternary Behaviour</title> + <programlisting role="php"> +<![CDATA[ +<?php +// on first glance, the following appears to output 'true' +echo (true?'true':false?'t':'f'); + +// however, the actual output of the above is 't' +// this is because ternary expressions are evaluated from left to right + +// the following is a more obvious version of the same code as above +echo ((true ? 'true' : 'false') ? 't' : 'f'); + +// here, you can see that the first expression is evaluated to 'true', which +// in turn evaluates to (bool)true, thus returning the true branch of the +// second ternary expression. +?> +]]> + </programlisting> + </example> + </para> + </note> </sect2> </sect1>