For Programmers: Free Programming Magazines  


Home > Archive > PHP Documentation > September 2006 > cvs: phpdoc /en/language operators.xml









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author cvs: phpdoc /en/language operators.xml
Sean Coates

2006-09-01, 6:57 pm

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/phpdo...3&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>
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com