PHP heredoc Syntax - Alternative to Assigning String Variables
Working with large strings of text in your php application can often look messy and clumsy. An alternative method is to use the heredoc syntax:
$longString = <<
The variable will be populated with this text. You can use other variables (e.g. $otherVariable) and html code.
EOT;
In the above example, the variable $longString will be populated with the text between the identifiers named “EOT”. It is important that the closing identifier is placed on a new line with no leading spaces, tabs or other characters. It is invalid to use the heredoc syntax in intializing class members.
The heredoc syntax is worth considering as an alternative when you are working with a lot of text, especially (x)html or xml. An added benifit is if you are using an IDE with syntax highlighting, the text within the identifiers will most likely still be highlighted.
