PHP Why Can't EOM contain PHP functions? [duplicate]

让人想犯罪 __ 提交于 2019-12-12 03:48:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!