问题
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);
回答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);
回答2:
What you're talking about are Heredocs and they don't support function interpolation, only variable interpolation.
来源:https://stackoverflow.com/questions/13917256/php-why-cant-eom-contain-php-functions