strip_tags() 给出“不能在写入上下文中使用函数返回值”不写字的时候
当尝试使用 empty(strip_tags($string))
作为三元语句中的条件进行赋值时,我收到错误 Can't use function return value in write context
。因此,为了测试,我删除了赋值和三元语句。但当它显然不在写入上下文中时,我仍然收到此错误。
想想看,我不确定 write context
是什么意思——我认为这与赋值有关,但我不能说我肯定知道这一点。
为什么这不像我想象的那样有效?对我来说这似乎很简单。我缺少什么?
$ 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
empty()
要求您对变量进行操作,而不是对函数的输出进行操作。相反,请将strip_tags()
的输出存储到变量中。empty()
文档对此进行了解释:empty()
is requiring you to act on a variable, rather than the output of a function. Instead, store the output ofstrip_tags()
into a variable.This is explained in the
empty()
documentation:empty
需要一个变量。您传递一个函数返回值,这是不同的。您可以通过将其放入括号中来解决此问题:
但最好 就像迈克尔写的,这个建议是某种肮脏的黑客。如果有兴趣,已在括号改变函数调用结果的语义中进行了讨论和 PHP 错误报告:#55222 致命使用括号时错误消失。
empty
requires a variable. You pass a function return value, that's different.You can work around that by putting it into brackets:
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.