views:

63

answers:

3

Quick one for you guys.

Say I have a function that outputs a string:

function myString()
{
      echo 'Hello World';
}

How would I go about testing to see if the function outputs any data?

if(myString() ==''){ 
      echo ''Empty function;  
}
A: 

Sorry, I misunderstood the question. Output BUffer should be the way to go as explained by thephpdeveloper.

---NOT RELEVANT---

if(!myString()){ 
  echo 'Empty function';
}

will echo 'Empty Function' when myString returns a value that can be evaluated to false IE: o, false, null, "" etc...

if(myString() === NULL){ 
  echo 'Empty function';
}

Will only print 'Empty Function' when there is no return value.

Javier Parra
This is a good answer for checking return values, but I don't think this answers what the OP was asking. I think the OP wants to know if there is a way to determine if a function writes to the output stream.
Chris Thompson
+8  A: 

Using output buffer functions:

function testFunctionOutput($f, $p = array()){
    ob_start();
    call_user_func_array($f, $p);
    $s = ob_get_contents();
    ob_end_flush();
    return (bool)($s !== '');
}

So say...

function testa(){
  echo 'test';
}

function testb($b){
  $i = 20 * $b;
  return $i;
}

var_dump(testFunctionOutput('testa'));
var_dump(testFunctionOutput('testb', array(10)));

Alternative version suggested by Felix:

function testFunctionOutput2($f, $p = array()){
    ob_start();
    call_user_func_array($f, $p);
    $l = ob_get_length();
    ob_end_clean();
    return (bool)($l > 0);
}
thephpdeveloper
+1 But to only test whether a function generates output, `ob_end_clean` is better suited than `ob_end_flush` imo. Instead of `ob_get_contents`, `ob_get_length` can be used too.
Felix Kling
Thanks guys, but getting a "Warning: Missing argument 1 " error
Keith Donegan
Don't have functions echo output, return it instead. That way you're more flexible to do stuff like this. So when you just want to display it say print myFunction();
TravisO
@Keith - fixed. was the optional parameter issue.
thephpdeveloper
@TravisO , I have no control over the function as it's generated from a plugin in WordPress :(
Keith Donegan
+1  A: 

Usually if a function returns data it will do so in a return statement.

as in

function myString() {

 $striing = 'hello';
 return $string;

}

To test it just call the function and see what it returns.

If what you are asking is if something will be written to output as CT commented below ... You will need to do something like this:

//first turn out the output buffer so that things are written to a buffer
ob_start();

//call function you want to test... output get put in buffer.
mystring();

//put contents of buffer in a variable and test that variable
$string = ob_get_contents();

//end output buffer
ob_end()

//test the string and do something...
if (!empty($string)) {

 //or whatever you need here.
 echo 'outputs to output'
}

You can find out a lot more at http://php.net/manual/en/function.ob-start.php

DKinzer
I don't think this is what the OP is asking, see my other comment
Chris Thompson
Good point, CT, I've edited the suggestion to include that scenario.
DKinzer