Re: Constants inside strings, sometimes
| From: | Andi Gutmans | Date: | Wed, 18 Dec 2002 05:27:16 +0000 |
| Subject: | Re: Constants inside strings, sometimes | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-92572@lists.php.net to get a copy of this message | ||
At 05:16 PM 12/17/2002 +0000, Philip Olson wrote:
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',Hi, The support for "$arr[a]" is a special support to make working with array offsets and strings easier. The "{...}" support should allow you to put any PHP expression in there. Basically echo $arr[a] and "{$arr[a]}" should behave exactly the same. There's a difference between $arr[a] and "{$arr[b]}" as 'a' is a defined constant and 'b' isn't so PHP is behaving correctly. Andi'b' => 'banana', 'c' => 'cranberry');echo "a $arr[a] \n"; // appleecho "b {$arr[a]} \n"; // banana (???) echo "c {$arr['a']} \n"; // appleecho "d $arr[b] \n"; // bananaecho "e {$arr[b]} \n"; // banana and E_NOTICE for// undefined constantecho "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.