From: [EMAIL PROTECTED] Operating system: Windows XP Home Edition PHP version: 4.2.0 PHP Bug Type: Documentation problem Bug description: State explicitly "code $foo['bar'] and not $foo[bar]"
In the "arrays" section "http://www.php.net/manual/en/language.types.array.php", the documentation should state explicitly "code $foo['bar'] and not $foo[bar]". It's a small point, but there is already a page and a half of text trying to make this point, without actually stating it. Finally I figured out what makes this part of the documentation unclear; it's because it does not state what it is trying to explain, but just goes right ahead and explains it. Specifically, I think there are 2 places where the point needs to be made explicitly: 1) ----------------------------------------------------- CURRENT TEXT: Array do's and don'ts Why is $foo[bar] wrong? You might have seen the following syntax in old scripts: etc. ----------------------------------------------------- SUGGESTED TEXT Array do's and don'ts Use quotes around an associative array index. For example, code $foo['bar'] and not $foo[bar]. Why is $foo[bar] wrong? You might have seen the following syntax in old scripts: etc. ----------------------------------------------------- 2) ----------------------------------------------------- CURRENT TEXT $foo[bar] = 'enemy'; echo $foo[bar]; // etc This is wrong, but it works. Then, why is it wrong? The reason is that, 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: echo $arr[ foo(true) ]; ----------------------------------------------------- SUGGESTED TEXT $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 the undefined constant gets converted to a string of the same name. 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: echo $arr[ foo(true) ]; ----------------------------------------------------- Hugh W Prior -- Edit bug report at http://bugs.php.net/?id=17065&edit=1 -- Fixed in CVS: http://bugs.php.net/fix.php?id=17065&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=17065&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=17065&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=17065&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=17065&r=support Expected behavior: http://bugs.php.net/fix.php?id=17065&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=17065&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=17065&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=17065&r=globals