Php Heredocs
Developers who don’t use heredoc’s have really annoying code to edit.
Developers who don’t use heredoc’s have really annoying code to edit.
Too many prints or echos.
This article shows how 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>Hi,p>
<p>Your name is: $namep>
EOHTML;
echo $view;
Everything between the heredoc tags will be output, and the variable will echo it’s value.
SQL
Heredoc’s become especially useful with mysql queries:
$sql=<<<EOSQL
SELECT *
FROM $tbl_name
ORDER BY $order
EOSQL;
In conclusion heredoc’s really keep your code clean, when used properly.
Comments
You need to login to comment.