Jump to content

A Php Heredoc Primer


TCH-Don
 Share

Recommended Posts

Recently someone asked what is heredoc?

 

In my opinion it is an often overlooked feature of php.

What is it you ask, well the short answer is, its a string wrapper.

Its grouped with the single and double qoute.

 

php functions like echo need to have stings wrapped in quotes,

like echo("some text to be displayed on the screen");

 

As you try to display more complex strings containing other qoutes,

you have to escape the double quotes that are part of the string by placing a \ in front of each " inside the first and last quotes.

 

Anything between single quotes will be displayed as is, meaning that php variables will be displayed as the name and not the contents.

 

So if you are going to use echo to output a link with a php variable in it,

it will not work by wrapping in single quotes.

You need to use double quotes and escape all internal double quotes.

 

 

An easier way is to use the heredoc wrapper <<<

 

Like the html tag PRE,

heredoc preserves line formatting including spaces.

this makes it quick and easy to define large amounts of text, html and php variables.

 

The syntax is all on a single line

 

function space <<< space and the delimiter of your choice

and then new line with your text, html and variables

using as many lines as you need

followed by a new line with the delimiter

 

and looks like this

 

><?php
echo <<< EOD
some text to display

   including leading spaces and blank lines

and "you" can mix quotes as needed.

EOD;

?>

 

The <<< is followed by your own delimiter,

I used a common one EOD to indicate End Of Data,

but it can be what ever you wish.

 

And note that the ending delimiter must be on a line by itself

with no spaces on that line.

You can follow it with a ;

 

 

 

So an example with a litte style extras would look like this

><?php
echo <<< EOD
<style type="text/css">
<!--
font-size: large;
text-align: center;
color: blue;
-->
</style>

Hello $visitorname, thanks for visiting.

I will get back to you at $visitor email

Please visit our <a href="http://domain.com">site again soon.</a>

EOD;

?>

 

See how much easier that is to read and create.

And you do not have to jump in and out of php to display variables.

 

 

Another example: assign a table with the contents of php variables from a user submitted form to a variable

 

><?php
$thank_you_message= <<< HEREDOC
<table border="0" cellspacing="0" cellpadding="0" width="100%">
 <tr>
   <td>
   Thank you $visitorname for contacting me.
   </td>
   <td>
   I will get back to you at $visitoremail as soon as possible.
   </td>
   <td>
   Please feel free to visit my other site
   <a href="http://myothersite">visit my other site</a> any time.
   </td>
 </tr>
</table>
HEREDOC;

?>

 

remember no spaces on the last line with the delimiter.

 

 

Yes you could jump out of php for the most part and jump back in to php to display variables, but thats a lot of trouble.

 

With heredoc you can freely mix text, html and php variables without worring about escaping quotes.

And you can use leading spaces and blank lines.

In fact most forums make heavy use of heredoc for templates.

So give it a try.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...