PHP: Fatal error: Can’t use method return value in write context

Just a quick post to help anyone struggling with this error message, as this issue gets raised from time to time on support forums.

The reason for the error is usually that you’re attempting to use empty or isset on a function instead of a variable. While it may be obvious that this doesn’t make sense for isset(), the same cannot be said for empty(). You simply meant to check if the value returned from the function was an empty value; why shouldn’t you be able to do just that?

The reason is that empty($foo) is more or less syntactic sugar for isset($foo) && $foo. When written this way you can see that the isset() part of the statement doesn’t make sense for functions. This leaves us with simply the $foo part. The solution is to actually just drop the empty() part:

Instead of:

if (empty($obj->method()))
{
}

Simply drop the empty construct:

if ($obj->method())
{
}

2 thoughts on “PHP: Fatal error: Can’t use method return value in write context”

  1. <font
    color="”
    size=”+6″
    face=””>

    Comment:
    Enter Code

    <?php
    if(isset(md5($captcha) != $_SESSION['randomnr2'])) {
    $error['captcha'] = "CAPTCHA error. Please try again";
    }
    //if (isset($_POST["vercode"]) != $_SESSION["vercode"] OR $_SESSION["vercode"]=='')
    //{
    // echo 'Incorrect verification code.‘; }
    else {
    // add form data processing code here
    echo ‘Verification successful.‘;
    };
    ?>

  2. Hi,

    If you’re asking why you’re getting the error here, it’s because you’re using md5() together with isset(). That does not make sense. The rest of the (commented out) code also seems rather weird, as you’re comparing the return value from isset() (which is a boolean) with the actual value in the $_SESSION.

Leave a Reply

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