HTML into PHP Variable (HTML 外部 PHP 代码)

2022-08-30 15:50:58

我是php的新手,想知道我是否可以拥有这样的东西:

<?php
 ...
 magicFunctionStart();
?>

<html>
   <head>...</head>
   <body>...</body>
</html>

<?php
 $variable = magicFunctionEnd();
 ...
?>

我现在必须使用的是

<?php
 ...
 $variable = "<html><head>...</head><body>...</body></html>"
?>

这很烦人,不可读。


答案 1

你试过“输出缓冲”吗?

<?php
 ...
 ob_start();
?>

<html>
   <head>...</head>
   <body>...<?php echo $another_variable ?></body>
</html>

<?php
 $variable = ob_get_clean();
 ...
?>

答案 2

我想你想要heredoc语法。

例如:

$var = <<<HTML
<html>
   <head>
random crap here
</html>
HTML;

推荐