From 219600d52e037a938055139342138c474c99b598 Mon Sep 17 00:00:00 2001 From: Torben Wilson Date: Wed, 18 Jun 2003 18:44:18 +0000 Subject: [PATCH] Clarify that you do not always quote array keys (i.e. when you're using a variable or constant as a key). git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@132367 c90b9560-bf6c-de11-be94-00142212c4b1 --- language/types.xml | 89 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 15 deletions(-) diff --git a/language/types.xml b/language/types.xml index 96d93d6813..6c8b1ce804 100644 --- a/language/types.xml +++ b/language/types.xml @@ -1,5 +1,5 @@ - + Types @@ -1513,9 +1513,10 @@ $b = array_values($a); Why is <literal>$foo[bar]</literal> wrong? - 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: - 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 + bare string (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 bar, then PHP + will substitute in the string 'bar' and use + that. + + + This does not mean to always quote the + key. You do not want to quote keys which are constants or variables, as this will + prevent PHP from interpreting them. + + + + +]]> + + + + The output from the above is: + + + + + More examples to demonstrate this fact: @@ -1557,7 +1615,7 @@ print $arr[fruit]; // apple // 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 @@ print "Hello " . $arr['fruit']; // Hello apple error_reporting is turned down to not show them. - As stated in the syntax section, there must be an expression between the - square brackets ('[' and ']'). - That means that you can write things like this: + As stated in the syntax section, + there must be an expression between the square brackets + ('[' and ']'). That means + that you can write things like this: