student student - 2 months ago 12
PHP Question

Parse error: syntax error, unexpected '<<'(T_SL) on line 2 using php heredoc syntax

When I run this code on my editor, it returns an error.

My php version is 5.4.35

Code:

<?php
$name = <<<SQL my name is amit
SQL;
echo $name;
?>


Error:


Parse error: syntax error, unexpected '<<'(T_SL) on line 2


Can someone please explain why this error, or what my script is missing?

Thanks.

Answer

Heredoc syntax requires a newline character immediately after the identifier, before the string content:

A third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.

$name = <<<SQL
my name is amit
SQL;
echo $name;

demo

Comments