php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #20601 A simple syntax parse error
Submitted: 2002-11-23 14:01 UTC Modified: 2003-01-22 18:00 UTC
From: mort at dsl dot pipex dot com Assigned: philip (profile)
Status: Closed Package: Documentation problem
PHP Version: 4.3.0RC1 OS: Windows ME
Private report: No CVE-ID: None
 [2002-11-23 14:01 UTC] mort at dsl dot pipex dot com
When using single quotes (i dont know if they will be allowed to show here) (') inside double quote strings ("), a parse error is produced;

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

This happened on Windows ME, using the latest Apache 1.3, and all settings in php.ini are default.

This is very annoying, please fix it!

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-11-23 14:05 UTC] jan@php.net
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.php.net/how-to-report.php

If you can provide more information, feel free to add it
to this bug and change the status back to "Open".

Thank you for your interest in PHP.


can you include a short script?
 [2002-12-04 18:17 UTC] sniper@php.net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.


 [2002-12-04 19:03 UTC] philip@php.net
Btw, this happens when you do:

print "a foo $bar['blah'] eh";

Don't do that.  You can do either:

print "a foo {$bar['blah']} eh";
print "a foo $bar[blah] eh";
print "a foo " . $bar['blah'] . " eh";

But when outside of strings always quote your keys:

print $bar[blah];   // bad
print $bar['blah']; // good

Unless of course you defined blah as a constant earlier.  Anyway I'm making a faq out of this question and marking as a doc bug because this question comes up a lot especially since 4.1.0 (autoglobals) and 4.2.0 (register_globals default change).
 [2002-12-05 13:26 UTC] goba@php.net
The string type description includes a lengthy explanation of this AFAIK.
 [2002-12-05 13:49 UTC] philip@php.net
As it turns out, the string docs are wrong and contain the following in the example:


// This is wrong for the same reason
// as $foo[bar] is wrong outside a string. 
echo "This is wrong: {$arr[foo][3]}";


I'll rewrite this part of the documention too.  $foo[bar] is perfectly fine inside strings, CONSTANTS aren't seen in strings.  Anyway, this will be further explained with a more specific example too.  And a faq entry :)  This question comes up waaaaaaay too much these days.
 [2002-12-17 10:37 UTC] goba@php.net
Philip, please do not change that part of the documentation. **It is correct!**.

Try with this script:

<?php

error_reporting(E_ALL);
ini_set("display_errors", TRUE);

$arr['foo'][3] = 14;

echo "This is wrong: {$arr[foo][3]}";
echo "This is good: {$arr['foo'][3]}";

?>

For the first echo line, a NOTICE error is
echoed out... So the documentation is correct. It may not be clear enough, but it is correct, the example is right.
 [2002-12-17 10:51 UTC] philip@php.net
Sort of.  This is a feature I was not aware of in PHP and imho is sort of a bug :)  

As it turns out, constants are only seen in strings if:

a) It's an array key
b) {braces} are around the array

So for example, NO E_NOTICE is generated from "a $arr[foo]"  but "a {$arr[foo]}" does!  And btw, "a {foo}" does not look for the constant foo.

And because multidimensional arrays inside strings require {braces} this is an important point.  IMHO this behavior of constants inside strings is inconsistent and I'm writing php-dev now! :)
 [2003-01-22 18:00 UTC] philip@php.net
Thanks for the report, the documentation has been updated here:
http://cvs.php.net/cvs.php/phpdoc/en/language/types.xml

On a related note, this may end up not being a parse error in the future.  The documentation will be updated once a verdict is made on bug #21820 so hold tight :)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 06 19:01:36 2025 UTC