Hello:

I am using heredoc syntax to build a confirmation message after a user submits a form. For one element, the description (represented by $desc), I want to use nl2br. How do I make this work? Here is what I tried, and it didn't work:

$conf_msg = <<<cnfmsg
<h1> $title </h1>
<p><strong> Submitted By: $user </strong> </p>
<p> {nl2br($desc)} </p>
cnfmsg;

It just printed the character literally.

Edit:

I made it work by applying nl2br to the variable outside the heredoc string. But I am still curious as to why the curly brackets didn't allow me to call the function inside the string?

Thanks

    A possible solution would be to use the function before the heredoc, i.e.

    $desc = nl2br($desc);
    $conf_msg = <<<cnfmsg
    <h1> $title </h1>
    <p><strong> Submitted By: $user </strong> </p>
    <p> $desc </p>
    cnfmsg;

      laser, thanks, that is what I did and it worked fine.

      But I am still interested to know why I could not call the function inside the string?

        The reason would be that the function cant be differentiated from normal text.

        It is possible, of course, but then PHP would need us to escape say, parentheses within double quoted or heredoc strings.

          in otherwords you can only interpolate variables. even with brackets.

            Write a Reply...