TCH-Don Posted October 24, 2004 Posted October 24, 2004 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. Quote
btrfld Posted October 24, 2004 Posted October 24, 2004 Great tutorial, Don. Very clear and nicely illustrated. I've seen the syntax a few times, but never really understood it until now. Thanks! Thumbs Up Quote
stevevan Posted October 24, 2004 Posted October 24, 2004 Don: That "someone" who asked was me, and I too have to echo my thanks for your primer! You explained it very well and it gave me a few ideas on how I can improve the code in my site. Thanks again! Thumbs Up Quote
TCH-Don Posted October 24, 2004 Author Posted October 24, 2004 You are welcome. And if you find a great way to use heredoc, let us know, we all can learn from each other. that is what family is for. Quote
borfast Posted October 24, 2004 Posted October 24, 2004 Nice reading, Don, well done Thumbs Up Thumbs Up I have always used heredoc extensively because it is very very usefull. I'm sure others will learn it and come to use it as well, thanks to your tutorial Thumbs Up Quote
TCH-Bruce Posted October 24, 2004 Posted October 24, 2004 Very good explaination Don! Thumbs Up Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.