strip_tags() 给出“不能在写入上下文中使用函数返回值”不写字的时候

发布于 2024-12-07 04:35:34 字数 577 浏览 0 评论 0原文

当尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

下壹個目標 2024-12-14 04:35:34

empty() 要求您对变量进行操作,而不是对函数的输出进行操作。相反,请将 strip_tags() 的输出存储到变量中。

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

empty() 文档对此进行了解释:

注意:
empty() 仅检查变量,因为其他任何内容都会导致解析错误。换句话说,以下内容将不起作用:empty(trim($name))

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

伴梦长久 2024-12-14 04:35:34

empty 需要一个变量。您传递一个函数返回值,这是不同的。

您可以通过将其放入括号中来解决此问题:

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

但最好 就像迈克尔写的,这个建议是某种肮脏的黑客。如果有兴趣,已在括号改变函数调用结果的语义中进行了讨论和 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文