I just spotted this, so apologies if it's already been answered.
PHP will try to treat first in $arr[first] as a constant. if it doesn't find
a constant with this name it will treat it as a string. There may be an
error reporting level which throws this up as an error. $arr['first'] is
therefore lees ambiguous and so safer (and may even be a nano second
faster).
Tim Ward
Senior Systems Engineer
Please refer to the following disclaimer in respect of this message:
http://www.stivesdirect.com/e-mail-disclaimer.html
> -----Original Message-----
> From: Yasuo Ohgaki [mailto:[EMAIL PROTECTED]]
> Sent: 20 March 2001 08:17
> To: [EMAIL PROTECTED]
> Subject: [Q] Parser behavior in a string
>
>
> I noticed that PHP4 accepts and does not raise error (notice
> messages) for
> following code.
>
> $arr = array('first' => 'one');
> $str = "This is first element of array: $arr[first]";
>
> Question: Is this expected behavior that PHP parse w/o errors?
>
> If this is not a expected behavior and subject to change, I'm
> not going to use
> array w/o ' or " for assoc arrays. Because I log all error
> messages for
> production system including notice messages.
>
> Thank you.
>
> [Note for those who are not familiar with use of {} in PHP]
> {} can be used to resolve ambiguity not only for arrays, but
> also for variables
> in strings.
>
> $str = "This is first element of array: {$arr['first']}"; //
> works as it should.
> $str = "This is first element of array: $arr['first']"; //
> parse error.
>
> You can use {} for multi-dimensional array and nested objects
> properties in
> strings also.
> i.e. "Text {$obj->foo->var} Text" works like 'Text '.
> $obj->foo->var .' Text'.
>
> --
> Yasuo Ohgaki
>
>
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]