note 72741 added to function.define
| From: | alf at osu1 dot php dot net | Date: | Wed, 31 Jan 2007 11:10:04 +0000 |
| Subject: | note 72741 added to function.define | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-122404@lists.php.net to get a copy of this message | ||
Couldn't see this beeing brought up, so I write it here
so all can be clear:
<?php
define('test', 8);
$foo[8] = "debug 1";
$foo['test'] = "debug 2";
print $foo[test]; // "debug 1"
print $foo['test']; // "debug 2"
?>
What happens when we use a string variable as key?
<?php
$str = "test";
print $foo[$str];
?>
One (wrong) way of reasoning:
=> $str outputs just test (without quotes)
=> $foo[$str] should then translate to $foo[test]
=> $foo[test] whould translate to $foo[8] (because test is a defined constant)
=> output should then be "debug 1"
This is wrong! ..but I'm sure I'm not the only one who thought about this.. ;)
Both $foo[$str] and $foo["$str"] outputs "debug 2"
----
Server IP: 129.16.214.50
Probable Submitter: 80.203.230.217
----
Manual Page -- http://www.php.net/manual/en/function.define.php
Edit -- https://master.php.net/note/edit/72741
Del: integrated -- https://master.php.net/note/delete/72741/integrated
Del: useless -- https://master.php.net/note/delete/72741/useless
Del: bad code -- https://master.php.net/note/delete/72741/bad+code
Del: spam -- https://master.php.net/note/delete/72741/spam
Del: non-english -- https://master.php.net/note/delete/72741/non-english
Del: in docs -- https://master.php.net/note/delete/72741/in+docs
Del: other reasons-- https://master.php.net/note/delete/72741
Reject -- https://master.php.net/note/reject/72741
Search -- https://master.php.net/manage/user-notes.php