[Solved]: Parse error: syntax error, unexpected T_SL
John Mwaniki / Updated on 07 Jul 2024When specifying a string in PHP using the Heredoc or Nowdoc syntax, you may encounter the error below.
Parse error: syntax error, unexpected T_SL on line xx
For example, the code in the three examples below would result in this error.
Example 1
<?php
$message = <<<EOD The quick brown fox jumps over the lazy dog
EOD;
?>
Example 2
<?php
$message = <<<HEREDOC
The quick brown fox jumps over the lazy dog
HEREDOC;
?>
Example 3
<?php
$message = <<ANYTHING
The quick brown fox jumps over the lazy dog
ANYTHING;
?>
Explanation for the error
In example 1, there are characters after the opening identifier.
In example 2, there is a whitespace after the opening identifier.
In example 3, there are only two angle brackets '<<'.
The Solution
Ensure there is no characters or space on the same line after the opening identifier. The opening identifier must be followed immediately by a new line. Since a space or tab is not easy to notice, click and drag the mouse after the identifier. If it will highlight anything then there is a space, remove it.
The heredoc operator consists of 3 angle brackets '<<<'. Omitting one or two will show this error.
That's it. With this, you are able to fix the error.