0

Suppose I've a PHP function returning some values.

<?php
function wpse_20131026() {
  $var = "I'm a function";
  return $var;
}
?>

How can I check whether it's returning empty or not? I tried the following:

<?php
if( !empty( wpse_20131026() ) ) {
  //do_something;
}
?>

But it's warning me:

Fatal error: Can't use function return value in write context

Mayeenul Islam
  • 4,532
  • 5
  • 49
  • 102

1 Answers1

3

empty() checks variables, but not values in php versions, that less than 5.5.

Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.

So you may use a temporary variable:

$var = wpse_20131026();

if(!empty($var)) {
    // do_something with $var
}

Or boolean evaluation directly (without empty() call):

if(wpse_20131026()) {
    // do_something
}
BlitZ
  • 12,038
  • 3
  • 49
  • 68