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.

Are you a Developer, Designer or Copy Writer? Or Looking For One?

Click here to see 100s of jobs updated every day that you can complete to earn good money. Also find that programmer, designer or copy writer you have been looking for - advertise and get the best guy for your job - click here to find out more

Leave a Reply

Make $100,000/Month - Every Month!

It Works! We Make Our Customers Millionaires! Act now and get $600 bonus and a Free Money Making Website! - click to get started