为什么这个表达式返回错误,我该如何解决? [重复]

问题描述:

This question already has an answer here:

This code:

if(!empty(trim($_POST['post']))){ }

return this error:

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

How can I resolve and avoid to do 2 checks ( trim and then empty ) ?

I want to check if POST is not only a blank space.

</div>

此问题已经存在 这里有一个答案: p>

  • 奇怪的PHP错误:'不能在写上下文中使用函数返回值' 10条答案 span> li> ul> div>

    此代码 : p>

      if(!empty(trim($ _ POST ['post']))){} 
      code>  pre> 
     
     

    返回此错误: p>

    致命错误:无法在写入上下文中使用函数返回值... p> blockquote>

    如何解决并避免进行2次检查(修剪然后清空)? p>

    我想检查POST是否只是一个空格。 p> div>

In the documentation it actually explains this problem specifically, then gives you an alternate solution. You can use

trim($name) == false.

if (trim($_POST['post'])) {

Is functionally equivalent to what you appear to be trying to do. There's no need to call !empty

you cant use functions inside isset , empty statements. just assign the result of trim to a variable.

$r = trim($_POST['blop']);

if(!empty($r))....

edit: Prior to PHP 5.5

if (trim($_POST['post']) !== "") {
    // this is the same
}

In PHP, functions isset() and empty() are ment to test variables.

That means this

if(empty("someText")) { ... }

or this

if(isset(somefunction(args))) { ... }

doesn't make any sence, since result of a function is always defined, e.t.c.

These functions serve to tell wether a variable is defined or not, so argument must me a variable, or array with index, then it checks the index (works on objects too), like

if(!empty($_POST["mydata"])) {
    //do something
} else {
    echo "Wrong input";
}