Apr 8 2010

Srsly PHP, why can’t I do this:

$value = isset($myGPCWrapper->Get("value")) ? $myGPCWrapper->Get("value") : $default;

Resulting in this:

Fatal error: Can’t use method return value in write context in /path/to/my/script.php on line 21

And instead I have to do this:

$foo = $myGPCWrapper->Get("value");
$value = isset($foo) ? $foo : $default;

or

$value = isset($_GET['value']) ? $myGPCWrapper->Get("value") : $default;

I mean… how is isset writing to my variable?!

3 Responses to “isset fail”

  1. David says:

    isset() isn’t a function. It’s a language construct that checks whether or not the given variable exists. You’re not passing isset() the name of a variable, you’re trying to pass it a value, returned by a function call.

    Shouldn’t it be the responsibility of $myGPCWrapper to ensure the integrity of the values it stores?

  2. David says:

    I didn’t explain very well… :/ Try this:

    isset(7)
    isset(chr(64))

    Both are passing values to isset(), rather than varaibles.

  3. James says:

    Thanks for the explanation:

    You’re not passing isset() the name of a variable, you’re trying to pass it a value, returned by a function call.

    That I think is the key. Sure $myGPCWrapper could integrity check – but I wanted to check if the value is passed in the query string at all – if not, then fall back to a value retrieved from session. It turns out $myGPCWrapper (incidentally, not written by me!) returns boolean false if value is not set, and a string otherwise so I ended up with:

    $value = $myGPCWrapper->Get("value") !== false ? $myGPCWrapper->Get("value") : $default;

Leave a Reply