jeroen Tue May 22 15:06:17 2001 EDT
Modified files:
/phpdoc/en/language types.xml
Log:
Added the 'why is $foo[bar] bad' section to arrays.
This closes bug #7387
Index: phpdoc/en/language/types.xml
diff -u phpdoc/en/language/types.xml:1.25 phpdoc/en/language/types.xml:1.26
--- phpdoc/en/language/types.xml:1.25 Sat May 19 13:45:31 2001
+++ phpdoc/en/language/types.xml Tue May 22 15:06:17 2001
@@ -970,6 +970,93 @@
provides an easy way to traverse an array.
</para>
</sect2>
+
+ <sect2 id="language.types.array.donts">
+ <title>Array do's and don'ts</title>
+
+ <sect3 id="language.types.array.foo-bar">
+ <title>Why is <literal>$foo[bar]</literal> wrong?</title>
+ <para>
+ You might have seen the following syntax in old scripts:
+ <informalexample>
+ <programlisting role="php">
+$foo[bar] = 'enemy';
+echo $foo[bar];
+// etc
+ </programlisting>
+ </informalexample>
+ This is wrong, but it works. Then, why is it wrong? The reason is
+ that, as stated in the <link linkend="language.types.array.syntax"
+ >syntax</link> section, there must be an expression between the
+ square brackets ('<literal>[</literal>' and '<literal>]</literal>').
+ That means that you can write things like this:
+ <informalexample>
+ <programlisting role="php">
+echo $arr[ foo(true) ];
+ </programlisting>
+ </informalexample>
+ This is an example of using function-output as the array index.
+ PHP knows also about constants, and you may have seen the
+ <literal>E_*</literal> before.
+
+ <informalexample>
+ <programlisting role="php">
+$error_descriptions[E_ERROR] = "A fatal error has occured";
+$error_descriptions[E_WARNING] = "PHP issued a warning";
+$error_descriptions[E_NOTICE] = "This is just an informal notice";
+ </programlisting>
+ </informalexample>
+ Note that <literal>E_ERROR</literal> is also a valid identifier,
+ just like <literal>bar</literal> in the first example. But the last
+ example is in fact the same as writing:
+ <informalexample>
+ <programlisting role="php">
+$error_descriptions[1] = "A fatal error has occured";
+$error_descriptions[2] = "PHP issued a warning";
+$error_descriptions[8] = "This is just an informal notice";
+ </programlisting>
+ </informalexample>
+ because <literal>E_ERROR</literal> equals <literal>1</literal>, etc.
+ </para>
+ <para>
+ Then, how is it possible that <literal>$foo[bar]</literal> yet works?
+ That is, because <literal>bar</literal> is due to it's syntax
+ expected to be a constant expression. However, in this case no
+ constant with the name <literal>bar</literal> exists. PHP now
+ assumes that you meant <literal>bar</literal> literally,
+ as the string <literal>"bar"</literal>, but that you forgot
+ to write the quotes.
+ </para>
+ <sect4>
+ <title>So why is it bad then?</title>
+ <para>
+ At some point in the future, the PHP team might want to add another
+ constant or keyword, and then you get in trouble. For example,
+ you can already not use the words <literal>empty</literal> and
+ <literal>default</literal> this way, since it are special keywords.
+ <!-- <jeroen>hmm... i'm doubting this myself. Finish it if you like</jeroen>
+ But probably
+ the most threatening
+ thing is yourself, or whoever will maintain the script. You'll
+ maybe get very strange behaviour, and
+ -->
+ </para>
+ <para>
+ And, if these arguments don't help: this syntax is simply deprecated,
+ and it might stop working some day.
+ </para>
+ <note>
+ <simpara>
+ When you turn <link linkend="function.error-reporting"
+ >error_reporting</link> to <literal>E_ALL</literal>,
+ you will see that PHP generates warnings whenever this construct
+ is used. (put the line <literal>error_reporting(E_ALL);</literal>
+ in your script)
+ </simpara>
+ </note>
+ </sect4>
+ </sect3>
+ </sect2>
<sect2 id="language.types.array.examples">
<title>Examples</title>
@@ -1194,14 +1281,6 @@
<sect2>
<title>Misc</title>
- <sect3 id="language.types.array.foo-bar">
- <title>Why is <literal>$foo[bar]</literal> wrong?</title>
- <para>
- Because it is.
-
-
- </para>
- </sect3>
</sect2>
- example multi-dim with $arr[bla][bla] syntax