note 40101 added to language.operators.array

From: Date: Sun, 22 Feb 2004 01:49:22 +0000
Subject: note 40101 added to language.operators.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-65498@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/language.operators.array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40101 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40101&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40101&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#65498) next »