1
0
mirror of https://github.com/php/doc-en.git synced 2026-03-25 08:12:15 +01:00

Document dangling else handling

Cf. <https://bugs.php.net/51016#1619439579>.
This commit is contained in:
Christoph M. Becker
2021-11-16 16:10:56 +01:00
parent af64507ffd
commit 3725fe1c95

View File

@@ -37,6 +37,32 @@ if ($a > $b) {
linkend="control-structures.elseif">elseif</link>).
</para>
<note>
<title>Dangling else</title>
<para>
In case of nested <literal>if</literal>-<literal>else</literal> statements,
an <literal>else</literal> is always associated with the nearest <literal>if</literal>.
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$a = false;
$b = true;
if ($a)
if ($b)
echo "b";
else
echo "c";
?>
]]>
</programlisting>
</informalexample>
Despite the indentation (which does not matter for PHP), the <literal>else</literal>
is associated with the <literal>if ($b)</literal>, so the example does not produce
any output. While relying on this behavior is valid, it is recommended to avoid
it by using curly braces to resolve potential ambuigities.
</para>
</note>
</sect1>
<!-- Keep this comment at the end of the file