HEREDOC to Simplify Code Generation


/ Published in: PHP
Save to your folder(s)

Doing the echo with multiple concatenated bits of strings was very error prone and I was always getting parsing errors. The toggle format where you flip PHP on and off throughout the HTML is less error prone but is a real pain in the ass if you have short tags disabled. Those methods are okay for short pieces of code, but any long page just can't be easily generated that way.


Copy this code and paste it in your HTML
  1. <?php
  2. echo <<<'WOW'
  3. <pre style="display:inline-block;border:2px solid blue;border-radius:8px;padding:10px;">
  4. <form action="$formLink" method="get">
  5. <input name="name" type="text" value="$junk" />
  6. <aside>Now, I am printing some {$junk->stuff[1]}.<br />
  7. This should not print a capital 'A': \x41</aside>
  8. </pre>
  9. WOW;
  10. ?>

URL: http://coboldinosaur.com/pages/Keep_it_Simple_Using_HEREDOC.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.