PHP:抑制功能内的输出? [英] PHP: Suppress output within a function?

查看:100
本文介绍了PHP:抑制功能内的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

禁止函数可能产生的任何输出的最简单方法是什么?说我有这个:

$ p $ function testFunc(){
echo'Testing';
返回true;
}

我想调用testFunc()并获取其返回值, 显示在页面中。假设这是在 输出其他内容的其他代码的上下文中,是否有这样做的好方法?也许搞乱了输出缓冲区?

解决方案

是的,搞乱了输出缓冲区正是答案。在调用可输出的方法之前打开它(不是函数本身,而是在你调用它的地方,你可以将它包装到整个脚本或脚本流程中,但是可以通过包装在方法的调用周围):

  function foo(){
echoFlush!;
返回true;
}

ob_start();
$ a = foo();
ob_end_clean();

并且没有输出。


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?

解决方案

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):

function foo() {
  echo "Flush!";
  return true;
}

ob_start();
$a = foo();
ob_end_clean();

And no output is generated.

这篇关于PHP:抑制功能内的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆