有什么方法可以在PHP函数中返回HTML? (不将返回值构建为字符串) [英] Is there any way to return HTML in a PHP function? (without building the return value as a string)
本文介绍了有什么方法可以在PHP函数中返回HTML? (不将返回值构建为字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个PHP函数,用于输出标准HTML块.当前看起来像这样:
I have a PHP function that I'm using to output a standard block of HTML. It currently looks like this:
<?php function TestBlockHTML ($replStr) { ?>
<html>
<body><h1> <?php echo ($replStr) ?> </h1>
</html>
<?php } ?>
我想返回(而不是回显)函数内部的HTML.有没有办法在字符串中不构建HTML(以上)的方法?
I want to return (rather than echo) the HTML inside the function. Is there any way to do this without building up the HTML (above) in a string?
推荐答案
You can use a heredoc, which supports variable interpolation, making it look fairly neat:
function TestBlockHTML ($replStr) {
return <<<HTML
<html>
<body><h1>{$replStr}</h1>
</body>
</html>
HTML;
}
但是请特别注意手册中的警告-结束线不能包含任何空格,因此不能缩进.
Pay close attention to the warning in the manual though - the closing line must not contain any whitespace, so can't be indented.
这篇关于有什么方法可以在PHP函数中返回HTML? (不将返回值构建为字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文