<?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
<?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
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;
https://repl.it/@CiscoTest/PHP-Heredocs-lesslessless
<?php
//Heredocs start with <<< and a token ended with semi-colon
print <<< ENDHEREOK
We used ENDHEREOK "as" our token
Looks like it just "print"
things "as" it is. Let me loooook at what I just typed
I may add some more! I m gonna end it using ENDHEREOK but any token can be used
Give it a "try"! Also pay attention to so many double quotes because it is mandatory!
Also yes "if" you put
space after token(ENDHEREOK) above, you will get an error, just hit enter key after token!
Try this on repl.it
https://repl.it/@CiscoTest/PHP-Heredocs-lesslessless
ENDHEREOK;
?>