28

This code is part of a websocket server:

$msgArray = json_decode($msg);
if ($msgArray->sciID) {
  echo "Has sciID";
}

It will either be receiving a json string like {"sciID":67812343} or a completely different json string with no sciID such as {"something":"else"}.

When the server receives the later, it echos out: Notice: Undefined property: stdClass::$sciID in /path/to/file.php on line 10

What is the correct code to check if $msgArray->sciID exists?

2
  • if (isset($msgArray->sciID)) ... Commented May 1, 2011 at 23:13
  • possible duplicate of Testing if property exists Commented May 1, 2011 at 23:16

5 Answers 5

68

Use isset as a general purpose check (you could also use property_exists since you're dealing with an object):

if (isset($msgArray->sciID)) {
    echo "Has sciID";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note: With isset() and empty() a non-existant variable and a variable set to NULL are treated the same. Only property_exists and ReflectionObject will tell the difference.
7

property_exists()?

Comments

6

In case isset() or property_exists() doesn't work, we can use array_key_exists()

if (array_key_exists("key", $array)) {
    echo "Has key";
}

1 Comment

JSON string must be decoded as an array by setting second parameter true like json_decode($str, true) to use array_key_exists
2

I've always done isset() but I've recently changed to !empty() and empty because one of my friends suggested it.

1 Comment

In this case empty doesn't work (php -r 'class A {} $a = new A; var_dump(isset($a->b), empty($a->b));' returns TRUE and FALSE respectively), however I'm not sure why - probably because an undefined property doesn't equate to NULL or FALSE or anything else considered 'empty' as trying to access it throws a notice. Personally I like to distinguish between what I'm checking - if I'm checking for 0, null or an empty string I use ==0, is_null and strlen because of the issue above.
0

The isset() function checks if a variable is set and is not null, which is really stupid, given its name. If you only want to know if a variable is set, use the following function:

function is_set( & $variable ) {
    if ( isset( $variable ) and ! is_null( $variable ) )
        return true;
    else
        return false;
}

This function will return true exactly when the variable is not set, and false otherwise. The '&' next to the argument is important, because it tells PHP to pass a reference to the variable, and not the value of the variable, thus avoiding a "Notice: Undefined variable" when the variable is not set.

2 Comments

This will return true if the variable is defined and not null. That's exactly what isset() does by default.
Nostalg is correct; the is_null will return true in both cases, not helping you resolve the nature of the null. However this may help resolve the "notice". It certainly allows you to put the complex logic in a function and pass the potentially not existing variable to it without generating the warning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.