Fixing ‘Can’t use function return value in write context’ in PHP

You might have encountered this error in one of your PHP scripts. Although the reason for this problem is a testament to how completely messed PHP is, it is quite simple.

Let’s take a look at this function for example:

if(empty(trim($var))){ //Can't use function return value in write context
    ...

The reason is that empty() is not a function but a language construct, and will only work on variables, not function return values.

This would work just fine:

$var = trim($var);
if(empty($var)){ //All good
    ...

That’s PHP for you.

4 comments on “Fixing ‘Can’t use function return value in write context’ in PHP

  1. The empty function (before 5.4) can’t access to temporary reference. That’s why you need to save your value in another variable.

    But “empty()” is “useless” in PHP :

    if(empty($test)){ // result }
    is strictly equal to :
    if(!isset($test) || !$test){ // result}

    And in the other way :

    if(!empty($test)){ // result }
    is equal to :
    if($test){ // result }

  2. Hello .gud evening.
    ..how can I sort out this error….”
    Can’t use function return value in write context…..at line….”
    The piece of code giving this error is
    ……if(!empty(trim($current_password))) && !empty(trim($old_password)))……. {}…I’m using Php 6.0 and wamp 5.5

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax