Why do these Heredoc and Nowdoc cause errors?
Asked Answered
L

7

6

I've already found some solutions, but can't know what happened...

Example 1:

<?php

echo <<< EOD
test
EOD;

Example 2:

<?php

echo <<< 'EOD'
test
EOD;

Output 1,2:

PHP Parse error:  syntax error, unexpected end of file, expecting variable (T_VARIABLE) or heredoc end (T_END_HEREDOC) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN)

Example 3:

<?php

echo <<< EOD
test
EOD;

?>

Example 4:

<?php

echo <<< 'EOD'
test
EOD;

?>

Example 5:

<?php

echo <<< EOD
test
EOD;
'dummy';

Example 6:

<?php

echo <<< 'EOD'
test
EOD;
'dummy';

Output 3,4,5,6:

test
Landan answered 25/5, 2013 at 1:48 Comment(0)
B
8

You probably have spaces after the terminator in your first two examples, e.g.

EOD;[space]

With this:

<?php
echo <<<EOL
test
EOL;[space]

I get your error message, but WITHOUT the space, there's no error. And that's true whether there's a closing ?> or not.

Beetlebrowed answered 25/5, 2013 at 2:29 Comment(2)
It seems to be ideone's problem... Work ideone.com/9kpACT Fail ideone.com/LsRcqp Thank you anyway!!Landan
Also note that a space at the first like "<<<EOL[space]" will also cause ideone to fail.Mauri
P
5

The closing delimiter must start on the first column, no spaces nor tabs allowed in front of it. Be aware that things are changing in 5.5 and 5.6

'EOD' is equivalent to echo ' ',
"EOD" is equivalent to echo " " about variable substitutions.

The closing delimiter doesn't take any single of double quotes.

Pedicel answered 9/2, 2015 at 14:49 Comment(0)
R
2

Heredoc is very finicky. Make sure there are no spaces after your initial EOD. And the final EOD; must be on it's own line, with no spaces before it, and nothing else on that line. It's those spaces that give most people trouble.

And the quotes around 'EOD' aren't necessary.

Rigdon answered 25/5, 2013 at 6:47 Comment(1)
'EOD' means not Heredoc but Nowdoc. php.net/manual/en/…Landan
C
2

It took me sometime to master heredoc and nowdoc. Heredoc is a very tricky business. Here is the trick, the terminators should always be on singles lines except for the beginning terminator where you can add a variable or echo the string out and dont forget its is also not allowed to indent the closing tag

<?php

$string = <<<EXP // There should be nothing following this terminator <<<EXP
    You string goes here and it can include commas, quotes etc
EXP;// This should be on a single line without anything else. Hit enter to make it     contain      this line, even if there is nothing to write, still hit enter, because it     should be the master of this line

$string = <<<EXP2
I am a developer.
EXP2// Hit enter, please always do

echo $string // Works perfect

?>
Chaplin answered 18/2, 2014 at 20:11 Comment(1)
"Always Hit enter after the terminator" is the trick, otherwise we will see "Parse error: syntax error, unexpected end of file"Cater
F
1

One more thing: the closing tag is not "whatever you give", it must be a valid identifier with alphanumerics only (underscores are fine), if you give it any other character you'll be sorely disappointed.

Look:

$str = <<<'HEY WHAT THE HECK'

    This will not work
    and doesn't help you understand why

HEY WHAT THE HECK
;

PHP will give you a syntax error. Now if you remove the spaces,

$str = <<<'HEYWHATTHEHECK'

    Suddenly it's all peaches & cream

HEYWHATTHEHECK
;

(I just tried to use some UUID to make sure it won't happen inside the string. After 25 minutes of swearing I finally realized this.)

Happy nowdocking!

Fingered answered 25/6, 2016 at 19:23 Comment(0)
C
1

For those who may be on a desperate search here: See your php version. On 7.2 you NEED to close the heredoc on the first column OF THE LINE. On 7.4 u'r allowed to close it on the same column you start your variable.

Ex: (that's ok on 7.4)

public function html_blq(){
    $htmlBas = $this->html_bas();
    $htmlEsp = <<<HTML
        <div class="row endpointRow">
            <div class="row">ta</div>
            <div class="col-lg-3 col-xs-6"></div>
            <div class="col-lg-3 col-xs-6 form-group">
                $htmlBas 
            </div>    
            <div class="col-lg-3 col-xs-6">
                $htmlBas 
            </div>
            <div class="col-lg-3 col-xs-6"></div>
        </div>
    HTML; //pretty idented
}

(you need to do de follow on 7.2, otherwise you'll face a Unexpected EOF)

public function html_blq(){
    $htmlBas = $this->html_bas();
    $htmlEsp = <<<HTML
        <div class="row endpointRow">
            <div class="row">ta</div>
            <div class="col-lg-3 col-xs-6"></div>
            <div class="col-lg-3 col-xs-6 form-group">
                $htmlBas 
            </div>    
            <div class="col-lg-3 col-xs-6">
                $htmlBas 
            </div>
            <div class="col-lg-3 col-xs-6"></div>
        </div>
HTML; //ugly idented
}
Chrystalchryste answered 16/6, 2023 at 15:44 Comment(0)
A
0

I got an error of

( ! ) Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or heredoc end (T_END_HEREDOC) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in ....php on line 51

This happened when I did not have a newline after the end of the EOD;


    echo <<<EOD
<!DOCTYPE html>
<html>
<header>

</header>
<body>

<form method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" accept="text/csv" name="fileToUpload" id="fileToUpload" >
    <input type="submit" value="Upload CSV" name="submit">
</form>

</body>
</html>
EOD;

If ending the file at EOD; then it error out, add a newline / return.

php 7.2.28

Alexandriaalexandrian answered 18/3, 2020 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.