获取当前PHP的输出方法?

huangapple go评论60阅读模式
英文:

How to get the output of the current PHP?

问题

<?php
echo 'Hello';
echo 'World';

$e = // 获取页面的输出内容。

echo $e; // 应该打印出'HelloWorld'。
?>

我想要获取当前页面的所有输出并将其作为一个变量打印出来。

例如,我分别打印了'Hello'和'World'。如何获取页面上的内容?

英文:
&lt;?php
echo &#39;Hello&#39;;
echo &#39;World&#39;:

$e = // Get the output of the page.

echo $e; // &#39;HelloWorld&#39; should be printed.
?&gt;

I want to get all the output of the current page and print it as a variable.

For example, I printed 'Hello' and 'World' respectively. How can I get the content on the page?

答案1

得分: 1

使用输出缓冲:

ob_start();
echo 'Hello';
echo 'World';

$e = ob_get_clean();

echo $e; // 应该打印 'HelloWorld'。

更多信息可以在文档中找到:https://www.php.net/manual/en/ref.outcontrol.php

英文:

Use Output Buffering:

ob_start();
echo &#39;Hello&#39;;
echo &#39;World&#39;:

$e = ob_get_clean();

echo $e; // &#39;HelloWorld&#39; should be printed.

More information can be found in the documentation: https://www.php.net/manual/en/ref.outcontrol.php

huangapple
  • 本文由 发表于 2023年3月4日 04:05:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定