php - Finding Out if a Value in a Generated Array Exists Without Getting a Notice -
i have array, $beerarray
, obtained parsing data json api , putting php array. there values, $beer_name, expect in json data aren't there, causing value not exist in array. i've set if... else statements adjust these cases:
if (!($beerarray->response->beer->beer_name)) { } else { else } }
this prevents errors trying assign variable array value doesn't exist, still pesky notice:
notice: undefined property: stdclass::$beer_name in /users/x_/documents/html/php/populatebeer.php on line 66
is there better way structure logic avoid these notices? fills log false positives i'd avoid.
use isset() function. pass variable wish see set or not , function return true if variable exists or false if not.
for example change if statement to:
if (!isset($beerarray->response->beer->beer_name)) {
the first block executes if there no beer_name set. 2nd block executes if has name
here documentation: http://php.net/manual/en/function.isset.php
Comments
Post a Comment