svn: /phpdoc/en/trunk/language/ operators.xml

From: Date: Mon, 25 Jul 2016 08:43:12 +0000
Subject: svn: /phpdoc/en/trunk/language/ operators.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-14356@lists.php.net to get a copy of this message
bwoebi Mon, 25 Jul 2016 08:43:12 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=339710 Log: Move ?? null coalesce into its own section as it needs a few notes similar to ternary Changed paths: U phpdoc/en/trunk/language/operators.xml Modified: phpdoc/en/trunk/language/operators.xml =================================================================== --- phpdoc/en/trunk/language/operators.xml 2016-07-24 22:50:49 UTC (rev 339709) +++ phpdoc/en/trunk/language/operators.xml 2016-07-25 08:43:12 UTC (rev 339710) @@ -1319,14 +1319,6 @@ than <varname>$b</varname>. Available as of PHP 7. </entry> </row> - <row> - <entry>$a ?? $b ?? $c</entry> - <entry>Null coalescing</entry> - <entry> - The first operand from left to right that exists and is not &null;. - &null; if no values are defined and not &null;. Available as of PHP 7. - </entry> - </row> </tbody> </tgroup> </table> @@ -1584,7 +1576,7 @@ is important to know if you want to return a variable by reference. The statement <literal>return $var == 42 ? $a : $b;</literal> in a return-by-reference function will therefore not work and a warning is - issued in later PHP versions. + issued. </simpara> </note> <note> @@ -1616,7 +1608,67 @@ </para> </note> </sect2> + + <sect2 xml:id="language.operators.comparison.coalesce"> + <title>Null Coalescing Operator</title> + <para> + Further exists the "??" (or null coalesce) operator, available as of PHP 7. + <example> + <title>Assigning a default value</title> + <programlisting role="php"> +<![CDATA[ +<?php +// Example usage for: Null Coalesce Operator +$action = $_POST['action'] ?? 'default'; +// The above is identical to this if/else statement +if (isset($_POST['action'])) { + $action = 'default'; +} else { + $action = $_POST['action']; +} + +?> +]]> + </programlisting> + </example> + The expression <literal>(expr1) ?? (expr2)</literal> evaluates to + <replaceable>expr2</replaceable> if <replaceable>expr1</replaceable> is + &null;, and <replaceable>expr1</replaceable> otherwise. + </para> + <note> + <simpara> + Please note that the ternary operator is an expression, and that it + doesn't evaluate to a variable, but to the result of an expression. This + is important to know if you want to return a variable by reference. + The statement <literal>return $foo ?? $bar;</literal> in a + return-by-reference function will therefore not work and a warning is + issued. + </simpara> + </note> + <note> + <simpara> + Please note that the null coalesce operator allows for simple nesting: + <example> + <title>Non-obvious Ternary Behaviour</title> + <programlisting role="php"> +<![CDATA[ +<?php + +$foo = null; +$bar = null; +$baz = 1; +$qux = 2; + +echo $foo ?? $bar ?? $baz ?? $qux; // outputs 1 + +?> +]]> + </programlisting> + </example> + </simpara> + </note> + </sect2> </sect1> <sect1 xml:id="language.operators.errorcontrol">

« previous php.doc.cvs (#14356) next »