Sunday 10 June 2018

php - Why doesn't this work in production





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.


Answer



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;
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...