Constants inside strings, sometimes

From: Date: Tue, 17 Dec 2002 17:16:10 +0000
Subject: Constants inside strings, sometimes
Groups: php.dev 
Request: Send a blank email to php-dev+get-92565@lists.php.net to get a copy of this message
Hello, I stumbled upon this feature today and am wondering why it exists: <?php error_reporting(E_ALL); ini_set('display_errors', 1); define('a', 'b'); $arr = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry'); echo "a $arr[a] \n"; // apple echo "b {$arr[a]} \n"; // banana (???) echo "c {$arr['a']} \n"; // apple echo "d $arr[b] \n"; // banana echo "e {$arr[b]} \n"; // banana and E_NOTICE for // undefined constant echo "f {a} \n"; // {a} echo $arr[a]; // banana (expected) echo $arr['a']; // apple ?> In otherwords, constants are looked for inside strings under the following conditions: a) It's an array key b) {braces} are around the array This seems like inconsistent behavior as that's the only time they are looked for. This makes sense outside of strings but inside of them only if braces are used and with arrays? Please explain. It seems to me that if someone wants to use constants with array keys, don't put them in strings. Regards, Philip Olson

« previous php.dev (#92565) next »