开发者

PHP: Can I suppress output of a function?

So I know you can use output buffer. The problem right now is, I am using a function in a Wordpress plugin and it is automatically and it autom开发者_StackOverflow社区atically outputs the return. However, I want to check the return to see if it is false or returning my data.

I have tried:

if( function_name() ) {

}

$name = function_name();

I can still see the output in those situations, which I why I wanted to suppress it and do some checks first. I don't want to edit the core function of the plugin, but I will as a last resort. Is there a better work around?


Yes. It can be done like this:

ob_start();
if (function_name()) { }
else {}

// then you can do one of the following
ob_end_clean(); // in case you want to suppress function_name output
ob_flush(); // in case you don't want to suppress function_name output

Have a look here for more information about output control functions.

Also, instead of using ob_flush and ob_end_clean you could use ob_get_clean.


First, capture the output and return value of the function.

ob_start();
$name = function_name();
$output = ob_get_clean();

Next, decide whether or not you want to output it.

if ($name !== false) {
    echo $output;
}

You weren't clear what you wanted to do with the return value if it wasn't false or if it was actually the output of the function that you wanted to send to the page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜