liam cratchett liam cratchett - 3 months ago 15
PHP Question

Using empty gives Fatal Error: Can't use function return value in write context

This was for someones IPB forums and it keeps returning an error to some users, apparently the error is on line 447, which is

if ( !empty( trim( $message ) ) AND ( $message != '<p></p>' ) )
in the code below.

if ( \IPS\Member::loggedIn()->language()->checkKeyExists( "gallery_category_{$this->id}_permerror" ) )
{
try
{
$message = \IPS\Member::loggedIn()->language()->get( "gallery_category_{$this->id}_permerror" );
if ( !empty( trim( $message ) ) AND ( $message != '<p></p>' ) )
{
return $message;
}
}
catch ( \Exception $e ) {}
}


The error given is:


Fatal error: Can't use function return value in write context in /home/*****/public_html/forum/applications/gallery/sources/C‌​ategory/Category.php on line 447 –

Answer

Prior to PHP 5.5 you could only use empty on variables. So doing something like empty(trim($message)) would give you such an error. The alternative is to use !isset($message) || !trim($message), which is semantically identical to empty(trim($message)) in PHP >= 5.5.0

See the PHP manual for empty in the changelog:

5.5.0 empty() now supports expressions, rather than only variables.

If you're using a version of PHP less than 5.6.0 right now you should note that it is no longer supported. See the list of EOL PHP versions and the migration guide for upgrading.