PHP Nowdocs Posted by: John in PHP on
A few weeks ago Jordan blogged about the heredoc syntax. The point I want to raise in this blog, is the heredoc syntax parses PHP code, similar to double quotes around a string. For example

 
  1. $name = "John";
  2. $john = <<<EOD
  3. Hello World!<br />
  4. EOD;
  5.  
  6. $world = <<<EOD
  7. Hi $name!<br /><br >
  8. EOD;
Would output
Hello World!
Hi John!
Notice $name was parsed as a variable not as a literal. Until PHP 5.3 the only option to store and echo literal php code was to use single quotes around the code. However, you now have the option to use the nowdoc syntax. To use the nowdoc syntax all you have to do is place single quotes around the identifier. Using the nowdoc syntax our previous example becomes:
 
  1. $name = "John";
  2. $john = <<<'EOD'
  3.  
  4. Hello World!<br />
  5. EOD;
  6.  
  7. $world = <<<'EOD'
  8. Hi $name!<br /><br >
  9. EOD;
The output now is
Hello World!
Hi $name!

Trackback(0)
feed0 Comments

Write comment
 
 
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger
 

security image
Write the displayed characters


busy