0

Possible Duplicate:
Calling PHP functions within HEREDOC strings

I am using Swiftmailer PHP to create an order complete page. When the customer lands on this page, I use an include to a PHP file.

This page file has SwiftMailer EOM HTML that gets sent to the customer. However I have the HTML parts in chunks, so the header is via a function called header and order totals are the same too. I want to be able to include EOM functions inside the EOM. Is this possible?

Id  =       46088;

// MAIL FUNCTION
function mailToSend($Id){

// EOM CAN'T BE TABBED
$html = <<<EOM
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
getHeader($Id);
getOrderTotalBoxTable($Id);
</body>
</html>
EOM;


}

mailToSend(46088);
Community
  • 1
  • 1
TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209

2 Answers2

1

What you're talking about are Heredocs and they don't support function interpolation, only variable interpolation.

deceze
  • 510,633
  • 85
  • 743
  • 889
1

Like @deceze said, you can't, but this will work (extend @xenon comment to an example):

function getHeader($Id = '')
{
    $text = '';
    $text.=' Your first line of text, store it in an variable <br>';
    $text.= 'Hello '.$Id.'<br>';
    $text.='Your last text to be returned<br>';
    return $text;
}

// MAIL FUNCTION
function mailToSend($Id){

    $getHeader = getHeader($Id);
    $getOrderTotalBoxTable = getOrderTotalBoxTable($Id);

    // EOM CAN'T BE TABBED
$html = <<<EOM
    <!DOCTYPE html>
    <html lang="en">
    <head>
    </head>
    <body>
    $getHeader;
    $getOrderTotalBoxTable;
    </body>
    </html>
EOM;

}

mailToSend(46088);
Radu Maris
  • 5,648
  • 4
  • 39
  • 54
  • Using this example, I have, example `$getHeader` as a function above, the code is like `function getHeader($oid){ echo 'hello'; }` so the email does send but it is blank at the function part, the page shows the `hello` do I need to put it in the global $html var?? – TheBlackBenzKid Dec 19 '12 at 10:16
  • I did not fully understood your question, but the function getHeader needs to return something, not print, something along these lines: "function getHeader($oid){ return 'hello '.$oid; }" – Radu Maris Dec 19 '12 at 10:57
  • Can you show me an example, can I just do loads of returns?? – TheBlackBenzKid Dec 19 '12 at 11:26
  • You cannot do loads of returns. – Radu Maris Dec 19 '12 at 11:34
  • Yes, so I made a $print variable inside the other function, and just did print="" and then print .="" etc and then then return $print is blank, but echo $print works... but echo does not include it inside the EOM – TheBlackBenzKid Dec 19 '12 at 11:36
  • Links with this for future reference : http://stackoverflow.com/questions/513511/returning-a-variable-from-a-function-in-php-return-not-working – TheBlackBenzKid Dec 19 '12 at 11:49
  • Why I use return it only allows me to display one result - if I have MySQL loop inside it.. return only allows it to be used once... – TheBlackBenzKid Dec 19 '12 at 13:25