note 40101 deleted from language.operators.array by didou
| From: | didou@php.net | Date: | Sun, 22 Feb 2004 03:03:42 +0000 |
| Subject: | note 40101 deleted from language.operators.array by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-65502@lists.php.net to get a copy of this message | ||
Note Submitter: An Expert
----
Contrary to what "jeffa at anony dot com"
said above, using the syntax:
<?php
$hash = (
key => 'value',
foo => 'bar',
);
?>
is not "good" syntax (even if it does work). It will result in an error being issued
(possibly an E_NOTICE) that reads:
Use of undefined constant key - assumed 'key'
---
Here is what the manual says (http://php.net/manual/en/language.types.array.php)
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:
<?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?>
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) 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.