user4679529 user4679529 - 2 months ago 7
PHP Question

Unexpected end of file using heredoc syntax

I am getting a php error


unexpected end of file


I really can't figure it out, I have spent almost 2 hours on it but nothing happened, I have read the php manual but no luck.

Here is my code

<?Php
$x=<<<msg
John:-'do you have new car?"<br>Lilly:-"yes I have BMW" msg;
echo $x;
?>


Regards : Tom

Answer

Tom, I think you did not read the manual carefully,

"Manual says that the closing indentifier of heredoc must be clean, but in your code, you did not follow this rule"

Your closing identifier msg; is not following the heredoc syntax rule.

=>Read this

Closing identifier must come in a new line with no other characters except the closing identifier followed by a semicolon (;). if this rule is broken and closing indentifier is not clean, php will continue looking for one if no indentifier is found at the end of the file then php will produce an error.

in your code , closing identifier Was not placed in the new line, that's why you were getting syntax errors.

Rewrite

   <?Php
   $x=<<<msg
   John:-'do you have new car?"<br>Lilly:-"yes I have BMW"
 msg;
  echo $x;
  ?>

See the demo https://eval.in/302325

Comments