This question already has an answer here:
I have a really simple line of code:
if(empty(trim($this->name)) {
$this->error = "Name required";
return false;
}
This code works splendidly on my development environment, but throws a fatal error in production:
Can't use function return value in write context
Why is this, and more importantly, what do I need to change in my development environment so that my code behaves the same (aka breaks) on the development environment and in production?
Many thanks.
</div>
In versions of PHP below 5.5 you can not use the return value of a function or method with empty()
. You have to do a "hack".
// Do only if your PHP version is lower than 5.5
$trimmedVal = trim($this->name);
if(empty($trimmedVal)) {
$this->error = "Name required";
return false;
}
This should work
$name = $this->name;
if(empty(trim($name))) {
$this->error = "Name required";
return false;
}
because you can't use return values in "empty()".
It seems like you already have written content to the browser. In PHP, functions must be evaluated before any writes to the browser occurs. This is impossible to do if you return something afterwards.
This, at least, was the case when I used PHP. It may work with more recent versions.