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 automatically outputs the return. However, I want to check the return to see if it is false or returning my data.

I have tried:

Read More
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?

Related posts

Leave a Reply

2 comments

  1. 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.

  2. 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.