38

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?

Community
  • 1
  • 1
Sophie Mackeral
  • 897
  • 4
  • 12
  • 21
  • 3
    on the first result in google i found that you can't pass a function return value to `empty`, must be a variable – urraka Jun 17 '13 at 01:20

1 Answers1

113

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))
Ken Herbert
  • 5,205
  • 5
  • 28
  • 37