A Php Heredoc Primer

liunx

Guest
Recently someone asked what is heredoc?<br /><br />In my opinion it is an often overlooked feature of php.<br />What is it you ask, well the short answer is, its a string wrapper.<br />Its grouped with the single and double qoute.<br /><br />php functions like echo need to have stings wrapped in quotes,<br />like echo("some text to be displayed on the screen");<br /><br />As you try to display more complex strings containing other qoutes,<br />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.<br /><br />Anything between single quotes will be displayed as is, meaning that php variables will be displayed as the name and not the contents.<br /><br />So if you are going to use echo to output a link with a php variable in it,<br />it will not work by wrapping in single quotes.<br />You need to use double quotes and escape all internal double quotes.<br /><br /><br />An easier way is to use the heredoc wrapper <<<<br /><br />Like the html tag PRE, <br />heredoc preserves line formatting including spaces.<br />this makes it quick and easy to define large amounts of text, html and php variables.<br /><br />The syntax is all on a single line<br /><br />function space <<< space and the delimiter of your choice <br />and then new line with your text, html and variables<br />using as many lines as you need<br />followed by a new line with the delimiter<br /><br />and looks like this<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><?php<br />echo <<< EOD<br />some text to display<br /><br />    including leading spaces and blank lines<br /><br />and "you" can mix quotes as needed.<br /><br />EOD;<br /><br />?><!--c2--></div><!--ec2--><br /><br />The <<< is followed by your own delimiter,<br />I used a common one EOD to indicate End Of Data,<br />but it can be what ever you wish.<br /><br />And note that the ending delimiter must be on a line by itself<br />with no spaces on that line.<br />You can follow it with a ; <br /><br /><br /><br />So an example with a litte style extras would look like this<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><?php<br />echo <<< EOD<br /><style type="text/css"><br /><!--<br />font-size: large;<br />text-align: center;<br />color: blue;<br />--><br /></style><br /><br />Hello $visitorname, thanks for visiting.<br /><br />I will get back to you at $visitor email<br /><br />Please visit our <a href=http://www.totalchoicehosting.com/forums/lofiversion/index.php/"http://domain.com">site again soon.</a><br /><br />EOD;<br /><br />?><!--c2--></div><!--ec2--><br /><br />See how much easier that is to read and create.<br />And you do not have to jump in and out of php to display variables.<br /><br /><br />Another example: assign a table with the contents of php variables from a user submitted form to a variable<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><?php<br />$thank_you_message= <<< HEREDOC<br /><table border="0" cellspacing="0" cellpadding="0" width="100%"><br />  <tr><br />    <td><br />    Thank you $visitorname for contacting me.<br />    </td><br />    <td><br />    I will get back to you at $visitoremail as soon as possible.<br />    </td><br />    <td><br />    Please feel free to visit my other site<br />    <a href=http://www.totalchoicehosting.com/forums/lofiversion/index.php/"http://myothersite">visit my other site</a> any time.<br />    </td><br />  </tr><br /></table><br />HEREDOC;<br /><br />?><!--c2--></div><!--ec2--><br /><br />remember no spaces on the last line with the delimiter.<br /><br /><br />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.<br /><br />With heredoc you can freely mix text, html and php variables without worring about escaping quotes.<br />And you can use leading spaces and blank lines.<br />In fact most forums make heavy use of heredoc for templates.<br />So give it a try.<!--content-->
Great tutorial, Don. Very clear and nicely illustrated. I've seen the syntax a few times, but never really understood it until now.<br /><br />Thanks! Thumbs Up<!--content-->
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.<br /><br />Thanks again! Thumbs Up<!--content-->
You are welcome.<br />And if you find a great way to use heredoc,<br />let us know, we all can learn from each other.<br />that is what family is for.<!--content-->
Nice reading, Don, well done Thumbs Up Thumbs Up<br />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<!--content-->
Very good explaination Don! Thumbs Up<!--content-->
 
Top