You learn something new every day

Stumble It!

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!

I'm happy to use Increase Sociability.