Home » Php » php – Can't use function return value in write context?

php – Can't use function return value in write context?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have this code:

public function __construct($Directory = null) 
{
    if ($Directory === null) {
        trigger_error("Directory Must Be Set!", E_USER_ERROR);
    }
    if (isset($Directory)) {
        if (!empty(trim($Directory))) { //Error Line
            echo "test"; 
        }
    }
}

(The echo is for my debugging purposes.)

I get returned with the fatal error:

Can’t use function return value in write context

According to PHP storm this returns:

Variable Expected

But using the code directly from this question:

White spaces throwing off HTML form validation

This is the correct syntax, as i’ve used it in the past… But, in this situation this is throwing an error. Why is this?

How to&Answers:

From the PHP docs on the empty function:

Note:
Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.

So you will have to split that line into something like the following:

$trimDir = trim($Directory);
if(!empty($trimDir))