torben          Wed Jun 18 14:44:18 2003 EDT

  Modified files:              
    /phpdoc/en/language types.xml 
  Log:
  Clarify that you do not always quote array keys (i.e. when you're using
  a variable or constant as a key).
  
  
Index: phpdoc/en/language/types.xml
diff -u phpdoc/en/language/types.xml:1.117 phpdoc/en/language/types.xml:1.118
--- phpdoc/en/language/types.xml:1.117  Wed Jun 18 14:19:07 2003
+++ phpdoc/en/language/types.xml        Wed Jun 18 14:44:18 2003
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="iso-8859-1"?>
-<!-- $Revision: 1.117 $ -->
+<!-- $Revision: 1.118 $ -->
  <chapter id="language.types">
   <title>Types</title>
 
@@ -1513,9 +1513,10 @@
     <sect3 id="language.types.array.foo-bar">
      <title>Why is <literal>$foo[bar]</literal> wrong?</title>
      <para>
-      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:
+      You should always use quotes around a string literal
+      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[
@@ -1527,13 +1528,70 @@
 ]]>
        </programlisting>
       </informalexample>
-      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
-      automatically for backward compatibility reasons. 
+      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 PHP automatically converts a
+      <emphasis>bare string</emphasis> (an unquoted string which does
+      not correspond to any known symbol, such as constants) into a
+      string which contains the bare string. For instance, if there is
+      no defined constant named <constant>bar</constant>, then PHP
+      will substitute in the string <literal>'bar'</literal> and use
+      that.
      </para>
+     <note>
+      <simpara>
+       This does not mean to <emphasis>always</emphasis> quote the
+       key. You do not want to quote keys which are <link
+       linkend="language.constants">constants</link> or <link
+       linkend="language.variables">variables</link>, as this will
+       prevent PHP from interpreting them.
+      </simpara>
+      <informalexample>
+       <programlisting role="php">
+<![CDATA[
+<?php
+error_reporting(E_ALL);
+ini_set('display_errors', true);
+ini_set('html_errors', false);
+// Simple array:
+$array = array(1, 2);
+$count = count($array);
+for ($i = 0; $i < $count; $i++) {
+    echo "\nChecking $i: \n";
+    echo "Bad: " . $array['$i'] . "\n";
+    echo "Good: " . $array[$i] . "\n";
+    echo "Bad: {$array['$i']}\n";
+    echo "Good: {$array[$i]}\n";
+}
+?>
+]]>
+       </programlisting>
+      </informalexample>
+      <para>
+       The output from the above is:
+       <screen>
+<![CDATA[
+Checking 0: 
+Notice: Undefined index:  $i in /path/to/script.html on line 9
+Bad: 
+Good: 1
+Notice: Undefined index:  $i in /path/to/script.html on line 11
+Bad: 
+Good: 1
+
+Checking 1: 
+Notice: Undefined index:  $i in /path/to/script.html on line 9
+Bad: 
+Good: 2
+Notice: Undefined index:  $i in /path/to/script.html on line 11
+Bad: 
+Good: 2
+]]>        
+       </screen>
+      </para>
+     </note>
      <para>
       More examples to demonstrate this fact:
       <informalexample>
@@ -1557,7 +1615,7 @@
 
 // Let's define a constant to demonstrate what's going on.  We
 // will assign value 'veggie' to a constant named fruit.
-define('fruit','veggie');
+define('fruit', 'veggie');
 
 // Notice the difference now
 print $arr['fruit'];  // apple
@@ -1593,10 +1651,11 @@
       error_reporting</link> is turned down to not show them.
      </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:
+      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">
 <![CDATA[



-- 
PHP Documentation Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to