php - How to throw an error message with json_decode? -


how can throw error message json_decode?

for instance,

$error = array(     "key_name" => "keyname - empty!",     "pub_name" => "pubname - empty!",     "path" => "path - empty!" );  $json = json_encode($error); $object = json_decode($json); print_r($object->keyname); 

i get,

notice: undefined property: stdclass::$key_namex in c:.... on line 32

keyname not exist actually, wonder if can check if condition,

if(!$object->keyname) { .... } 

is possible?

and there no error content,

$error = array( );  $json = json_encode($error); $object = json_decode($json); print_r($object->key_name); 

so thought of throwing error before proceeding codes follows,

if($object == '') {...} 

is possible?

you should prefer use property_exists() on isset().

as opposed isset(), property_exists() returns true if property has value null.

if( property_exists($object, 'keyname') ){     throw new exception( 'object key not exist.' ); //i prefer method    //or    trigger_error( 'object key not exist.', e_user_error ); } 

incidentally, same pattern should used arrays (array_key_exists preferred on isset same reason).


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -