Scroll To Top

Developers who don't use heredoc's have really annoying code to edit. Too many prints or echos. This article shows easy it is to use heredocs in php.

This is so easy I'm going to show you an example which will outline syntax.

$var_sql =<<<EOTXT
This is text
This is a $variable
This is a {$assoc_array['item']}
EOTXT; //this must not have tabs/spaces/anything before it

Now you know the syntax, let's see a better example:

$name = "Nick";

$view =<<<EOHTML
<p>Your name is: $name</p>

echo $view;

Everything between the heredoc tags will be output, and the variable will echo it's value.


Heredoc's become especially useful with mysql queries:

FROM $tbl_name
ORDER BY $order

In conclusion heredoc's really keep your code clean, when used properly.