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?

JJJollyjim
  • 5,837
  • 19
  • 56
  • 78

5 Answers5

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";
}
Ross
  • 46,186
  • 39
  • 120
  • 173
  • 3
    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. – dog May 01 '11 at 23:38
7

property_exists()?

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
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";
}
Petr Hurtak
  • 2,239
  • 1
  • 19
  • 18
  • 2
    JSON string must be decoded as an array by setting second parameter true like `json_decode($str, true)` to use array_key_exists – spetsnaz Jan 11 '17 at 08:46
2

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

Joshwaa
  • 840
  • 1
  • 11
  • 22
  • 1
    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. – Ross May 01 '11 at 23:42
  • thankyou bro, `!empty()` work fine in my case – ferrysyahrinal Mar 23 '22 at 12:52
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.

Sophivorus
  • 3,008
  • 3
  • 33
  • 43
  • This will return true if the variable is defined and not null. That's exactly what isset() does by default. – Nostalg.io Nov 17 '17 at 19:50
  • 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. – Gerard ONeill Jun 18 '21 at 04:20