[Q] Parser behavior in a string
| From: | Yasuo Ohgaki | Date: | Tue, 20 Mar 2001 08:16:42 +0000 |
| Subject: | [Q] Parser behavior in a string | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-44693@lists.php.net to get a copy of this message | ||
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