PHP:抑制函数内的输出?
抑制函数可能产生的任何输出的最简单方法是什么? 假设我有这个:
function testFunc() {
echo 'Testing';
return true;
}
我想调用 testFunc() 并获取其返回值,而页面中不显示“Testing”。 假设这将在其他输出其他内容的代码的上下文中,是否有一个好的方法可以做到这一点? 也许弄乱了输出缓冲区?
What is the simplest way to suppress any output a function might produce? Say I have this:
function testFunc() {
echo 'Testing';
return true;
}
And I want to call testFunc() and get its return value without "Testing" showing up in the page. Assuming this would be in the context of other code that does output other things, is there a good method for doing this? Maybe messing with the output buffer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,搞乱 输出缓冲区 正是答案。 只需在调用将输出的方法之前将其打开(不是函数本身,而是在调用它的地方,您可以将其包装在整个脚本或脚本流中,但您可以通过以下方式使其尽可能“紧密”)将其包装在方法的调用周围):
并且不会生成任何输出。
Yes, messing with the Output Buffer is exactly the answer. Just turn it on before you call your method that would output (not the function itself, but where you call it, you could wrap it around your whole script or the script flow, but you can make it as "tight" as possible by just wrapping it around the call of the method):
And no output is generated.
在这里:
“ob”代表“输出缓冲”,请查看此处的手册页:http:// www.php.net/outcontrol
Here you go:
"ob" stands for "output buffering", take a look at the manual pages here: http://www.php.net/outcontrol
是的,关于利用 PHP 的输出缓冲函数,即 ob_start 和 ob_end_clean (在 php.net 上查找它们),您的方向是正确的:
Yes you are on the right track as to leveraging PHP's output buffering functions, i.e. ob_start and ob_end_clean (look them up on php.net):
这不是就像在代码中应用一些条件一样简单吗?
我的意思是如果变量=测试则输出,否则不输出?
对于像 EVAL 这样结果直接输出到浏览器的函数,您可以在 ob_start 中捕获结果。
Isn't it as easy as applying some conditions to your code?
I mean if variable = testing then output, else don't?
For functions that have a result that outputs direct to the browser like EVAL, you could capture the result in an ob_start.