You learn something new every day

Last night, I was helping my wife with some PHP code for an email form. To create the email message, I had her use a heredoc, which is a convenient way to put a large block of text into a variable, while interpolating other variables into it. But it was giving us errors. What we discovered is that you can’t do this:


  $message = <<<MSG
  Hello $postvars['FirstName']
  MSG;

Even though heredocs can accept other complex variable substitutions, like $var->foo[1], it seems that the quoting on the index messes with the interpolation. So you have to use a more explicit syntax to help PHP figure out where the variables start and end:


  $message = <<<MSG
  Hello {$postvars['FirstName']}
  MSG;

Of course, this doesn’t just apply to heredocs, it applies to any interpolated (e.g., double-quoted) string in PHP.

I’m posting this as a future reference for myself, and for anyone else who might run across this problem in the future.

Stumble It!
You learn something new every day

Related posts:

  1. Commenting in IE6 problem fixed
    " Last month, my wife informed me that she couldn’t post comments on this site using IE6. I found a work-around, but never got around..."
  2. XSLT-Fu
    " I’ve recently started learning XSLT. The basics aren’t too hard, but as usual, I’ve been trying to move faster than I can absorb things...."
  3. Perl geekery: building hashes
    " Say we’re writing a program in Perl, and we need to pass a lot of data back and forth between subroutines. Using global variables..."
  4. KLEZ Redux
    "A friend of mine asked me how I filter KLEZ in my Outlook mail client. I have two rules for this, and they look like..."
  5. Syndication Bug
    " Brent just let me know that there is a problem with my XML/RSS/RDF (whatever you call it) syndication. The problem is that the permalink..."
This entry was posted in Tech and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Posted March 5, 2004 at 9:38 am | Permalink

    Thanks! I just drop the index quoting, likeso:
    Hello $postvars[FirstName]
    …which is not the best. I’ll start encapsulating in braces, now.

  2. Dougal dougal.gunters.org
    Posted March 5, 2004 at 9:52 am | Permalink

    Right, that will work, but it’s deprecated. I haven’t checked, but it might not work anymore in PHP 5 (unless the symbol inside the brackets is already defined as a constant).

  3. strick yak.net
    Posted March 7, 2004 at 10:10 pm | Permalink

    yes PHP yearns to be as unpredictable as Perl.

    ::little face of pathetic amusement::

Post a Comment

Your email is never published nor shared.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting

  • Subscribe

  • Follow Me

    Twitter  Facebook  Flickr  Last.fm  LinkedIn  StumbleUpon  Technorati  Delicious  
  • Referrals

    Sign up for Text Link Ads and earn money from your blog.
  • Lifestream