开发者

strip_tags() giving "can't use function return value in write context" when not writing

开发者 https://www.devze.com 2023-04-08 04:06 出处:网络
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 t

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开发者_JAVA百科 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


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


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.

0

精彩评论

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

关注公众号