mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Clearify the array-do's-and-don'ts - thanks to Hugh W Prior (#17065)
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@81397 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
6e0fc2fd11
commit
fe4d5f0621
1 changed files with 12 additions and 4 deletions
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
<!-- $Revision: 1.76 $ -->
|
||||
<!-- $Revision: 1.77 $ -->
|
||||
<chapter id="language.types">
|
||||
<title>Types</title>
|
||||
|
||||
|
@ -1204,7 +1204,9 @@ unset( $a[2] );
|
|||
<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:
|
||||
You should always use quotes around an associative array index.
|
||||
For example, use $foo['bar'] and not $foo[bar]. But why is $foo[bar]
|
||||
wrong? You might have seen the following syntax in old scripts:
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
|
@ -1214,8 +1216,14 @@ echo $foo[bar];
|
|||
]]>
|
||||
</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"
|
||||
This is wrong, but it works. Then, why is it wrong? The reason is that
|
||||
this code has an undefined constant (bar) rather than a string ('bar' -
|
||||
notice the quotes), and PHP may in future define constants which,
|
||||
unfortunately for your code, have the same name. It works, because the
|
||||
undefined constant gets converted to a string of the same name.
|
||||
</para>
|
||||
<para>
|
||||
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:
|
||||
|
|
Loading…
Reference in a new issue