Extrakun Extrakun -5 years ago 142
PHP Question

Can't use method return value in write context

I would think the following piece of code should work, but it doesn't (Edited: Now works in PHP 5.5+):

if (!empty($r->getError()))

is simply:

public function getError()
return $this->error;

Yet I end up with this error:

can't use method return value in write context

What does this mean? Isn't this just a read?

Answer Source

Note: This is a very high voted answer with a high visibility, but please note that it promotes bad, unnecessary coding practices! See @porneL's answer for the correct way.

Note #2: I endorse the suggestions to use @porneL's answer. When I wrote this answer three years ago, I merely meant to explain the nature of the error, not necessarily endorse the alternative. The code snippet below is not recommended.

It's a limitation of empty() in PHP versions below 5.5.

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

You'd have to change to this

// Not recommended, just illustrates the issue
$err = $r->getError();
if (!empty($err))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download