1

I got the error Can't use function return value in write context when trying to do assignment with empty(strip_tags($string)) as the conditional in a ternary statement. So for testing, I got rid of the assignment and the ternary statement. But I'm still getting this error when it's apparently not in a write context.

Come to think of it, I'm not sure what write context means -- I thought it had to do with assignment, but I can't say that I know that for sure.

Why doesn't this work like I think it should? It seems pretty straight-forward to me. What am I missing?

$ cat test.php
<?

$string = "<br/>";

if ( empty(strip_tags($string)) ) {
  echo "It's empty.\n";
} else {
  echo "It's not empty.\n";
}

$ php test.php
PHP Fatal error:  Can't use function return value in write context in test.php on line 5
user151841
  • 15,348
  • 28
  • 93
  • 156

2 Answers2

5

empty() is requiring you to act on a variable, rather than the output of a function. Instead, store the output of strip_tags() into a variable.

$stripped = strip_tags($string);
if ( empty($stripped) ) {
  echo "It's empty.\n";
} else {
  echo "It's not empty.\n";
}

This is explained in the empty() documentation:

Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).

Michael Berkowski
  • 253,311
  • 39
  • 421
  • 371
4

empty requires a variable. You pass a function return value, that's different.

You can work around that by putting it into brackets:

if ( empty((strip_tags($string))) ) {

But better do like Michael wrote, this suggestion is some kind of a dirty hack. If interested, has been discussed in Parentheses altering semantics of function call result and the PHP Bug Report: #55222 Fatal Error disappears when using paranthesis.

Community
  • 1
  • 1
hakre
  • 178,314
  • 47
  • 389
  • 754