Question

I am using HEREDOC to include some text into the body of a mail PHP function.

However, I get this error:

"PHP Parse error: syntax error, unexpected T_SL in..."

The errors refers to the HEREDOC part of the included file:

<?php
$message = <<<EOD <html></body>Dear $firstName $lastName,
Sincerely,
Customer Service
EOD;?>

Is there any issue with the way I am writing this?

Thanks!

Was it helpful?

Solution

<?php
$message = <<<EOD
    <html><body>Dear {$firstName} {$lastName},
    Sincerely,
    Customer Service
EOD;
?>

With heredoc syntax there should be nothing except a new line after the opening identifier <<<EOD not even whitespace and you want to wrap your variables in curly braces. The closing identifierEOD; should have no whitespace before it and should be followed by a new line.

PHP Heredoc

OTHER TIPS

<?php
$message = <<<EOD
<html></body>Dear $firstName $lastName,
Sincerely,
Customer Service
EOD;
?>

Change

EOD;?>

to

EOD;
?>

Why?

Quoting from PHP docs:

It is very important to note that the line with the closing identifier must contain no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top