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',
             '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.
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



--
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to