Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heredoc not working

Tags:

php

heredoc

<?php

$information = <<<INFO 
Name: John Smith
Address: 123 Main St
City: Springville, CA
INFO;

echo $information;

?>

Result:

Parse error: syntax error, unexpected T_SL on line 3

like image 570
Enemy of the State Avatar asked Sep 17 '10 23:09

Enemy of the State


3 Answers

The parser is complaining because you have whitespace after the angled brackets declaring a heredoc. You need to make sure you're actually following the heredoc syntax, which you can find on the PHP Manual site (specifically: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc).

<?php
$information = <<<ENDHEREDOC
this is my text
ENDHEREDOC;
echo $information;
like image 96
Robert Elwell Avatar answered Oct 14 '22 09:10

Robert Elwell


Heredoc syntax has some strict rules we have to consider;

1 - There shouldn't be any character after opening identifier

True

"$a = <<<HEREDOC"

False

"<<<HEREDOC   "   //Remove space after opening identifier;

2 - There shouldn't be any other character after and before closing identifier except delimiter semicolon ; at the end. Also no indentation is allowed.

True

"HEREDOC;"

False

"HEREDOC  ;"   //Remove space between HEREDOC and ;

False

" HEREDOC;"   //Remove space before HEREDOC

False

"HEREDOC; "   //Remove space after ;

Heredoc string. END;

like image 32
Erdinç Çorbacı Avatar answered Oct 14 '22 09:10

Erdinç Çorbacı


I've just edited your question and fixed invalid formatting (SO is using Markdown). I found out that there is a space character after <<<INFO - that causes the error.

Delete that space and everything should work fine... well - it has to works fine.

like image 28
Crozin Avatar answered Oct 14 '22 09:10

Crozin