title: Calling PHP functions within HEREDOC strings
body: <p>In PHP, the HEREDOC string declarations are really useful for outputting a block of html.  You can have it parse in variables just by prefixing them with $, but for more complicated syntax (like $var[2][3]), you have to put your expression inside {} braces.</p>
  
  <p>In PHP 5, it <em>is</em> possible to actually make function calls within {} braces inside a HEREDOC string, but you have to go through a bit of work.  The function name itself has to be stored in a variable, and you have to call it like it is a dynamically-named function.  For example:</p>
  
  <pre><code>$fn = 'testfunction';
  function testfunction() { return 'ok'; }
  $string = &lt;&lt;&lt; heredoc
  plain text and now a function: {$fn()}
  heredoc;
  </code></pre>
  
  <p>As you can see, this is a bit more messy than just:</p>
  
  <pre><code>$string = &lt;&lt;&lt; heredoc
  plain text and now a function: {testfunction()}
  heredoc;
  </code></pre>
  
  <p>There are other ways besides the first code example, such as breaking out of the HEREDOC to call the function, or reversing the issue and doing something like:</p>
  
  <pre><code>?&gt;
  &lt;!-- directly output html and only breaking into php for the function --&gt;
  plain text and now a function: &lt;?PHP print testfunction(); ?&gt;
  </code></pre>
  
  <p>The latter has the disadvantage that the output is directly put into the output stream (unless I'm using output buffering), which might not be what I want.</p>
  
  <p>So, the essence of my question is: is there a more elegant way to approach this?</p>
  
  <p><b>Edit based on responses:</b> It certainly does seem like some kind of template engine would make my life much easier, but it would require me basically invert my usual PHP style.  Not that that's a bad thing, but it explains my inertia..  I'm up for figuring out ways to make life easier though, so I'm looking into templates now.</p>