language/control-structures/else.xml
690c3ea7c7d416c55e2c54d90bfd1f7f4d489288
...
...
@@ -37,6 +37,32 @@ if ($a > $b) {
37
37
linkend="control-structures.elseif">elseif</link>).
38
38

39
39
</para>
40
+
<note>
41
+
<title>Dangling else</title>
42
+
<para>
43
+
In case of nested <literal>if</literal>-<literal>else</literal> statements,
44
+
an <literal>else</literal> is always associated with the nearest <literal>if</literal>.
45
+
<informalexample>
46
+
<programlisting role="php">
47
+
<![CDATA[
48
+
<?php
49
+
$a = false;
50
+
$b = true;
51
+
if ($a)
52
+
if ($b)
53
+
echo "b";
54
+
else
55
+
echo "c";
56
+
?>
57
+
]]>
58
+
</programlisting>
59
+
</informalexample>
60
+
Despite the indentation (which does not matter for PHP), the <literal>else</literal>
61
+
is associated with the <literal>if ($b)</literal>, so the example does not produce
62
+
any output. While relying on this behavior is valid, it is recommended to avoid
63
+
it by using curly braces to resolve potential ambiguities.
64
+
</para>
65
+
</note>
40
66
</sect1>
41
67

42
68
<!-- Keep this comment at the end of the file
43
69