Nov
11
2011

Heredocs in PHP, what are they?



Heredocs in PHP, what are they?

We have already seen two kinds of strings in our previous posts. These are the simple ones, single quoted, and double quoted strings. We saw how to define the strings, and have them placed into a variable, for later displaying. How about if we have a very long string, that eventually will need all these quotation marks and a lot more other stuff. Here is where heredocs come in, and are very useful.

At a certain point, I had read about this subject, while reading one of my PHP books. I never really gave it much attention, since I asked myself, why would anyone use this, if it is easier to just use single or double quotes? I was wrong, but I didn’t notice till someone asked for help on this matter. I saw that the person using heredoc, was placing a huge amount of string, that included HTML tags, their attributes, and some PHP variables to go with it. Once I saw that this person was having a problem and needed help, I felt the need to read up on this subject again. I just didn’t know how it was called, so it took me a few minutes to get the subject.

How to declare heredocs

There are a few ways to declare a heredoc. Let’s create a string named $message with the token called msg.

Note that we started the heredoc with a token msg. This token has three (less than signs) in front of it. We also finish the heredoc with this same token. These tokens are arbitrary, and you just need to make sure, that this token does not appear within your block of string. Also, the token name can be anything you wish. I would recommend you make them relevant to what you are writing, just to be on the safe side. One thing to ‘NEVER FORGET’ is, when writing the closing token, it MUST NOT contain any characters before or after it, not even a SPACE. This will cause an error to be displayed to your screen, and saying it is in one line, when it really isn’t. You will break your head trying to figure out what the error is.
The above example is very simple, and it is equivalent to:

So what are these heredocs good for? These heredocs are great when it comes to long strings such as a sql statement, or even an email string that you would like to send through the mail() function. Observe a simple sql statement:

As you see, you can use variables inside the string statement. To call these variables, you just type it in normally. In order to get the value of a certain sport in an associative array, then you can do it like you see above, with the open and close curly brace.

For an email message containing HTML in it, you can do it as follows:

The above is an email message sent to the user when that user registered on the website.

The following is a table of escaped characters that you can use within a heredoc statement.
This table is from php.net:http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double

Escaped characters
Sequence Meaning
\n linefeed (LF or 0x0A (10) in ASCII)
\r carriage return (CR or 0x0D (13) in ASCII)
\t horizontal tab (HT or 0×09 (9) in ASCII)
\v vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
\f form feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
\\ backslash
\$ dollar sign
\” double-quote
\[0-7]{1,3} the sequence of characters matching the regular expression is a
character in octal notation
\x[0-9A-Fa-f]{1,2} the sequence of characters matching the regular expression is a
character in hexadecimal notation

Hope you all Enjoyed!



Related Posts

About the Author: Jorge Torres

MS. Computer Science Codeigniter Web Developer

1 Comment+ Add Comment

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

Leave a comment