What's the easiest way to echo $result->data['id']
that won't throw an error?
$result = someBackendCall(1, 2, 3);
if($result && isset($result->data) && isset($result->data['id'])){
echo $result->data['id'];
}
What's the easiest way to echo $result->data['id']
that won't throw an error?
$result = someBackendCall(1, 2, 3);
if($result && isset($result->data) && isset($result->data['id'])){
echo $result->data['id'];
}
Its sort of subjective, as there are various ways, and some people find one way easier than another. What you wrote can even be considered easiest by some.
Also, for example, you could just do:
echo (isset($result->data['id'])?$result->data['id']:'');
Or, you could put it in a standard if block:
if (isset($result->data['id'])) { echo $result->data['id']; }
Another way, would be to make a tiny little helper function that floats about that you reuse:
function echoVar(&$input=null) { if (isset($input)) { echo $input; } }
echoVar($result->data['id']);
echoVar($result->data['name']);
I've not really used the mini function method, so I cannot say for sure if its really any easier, since it really obscures your code reading it later.
One thing to note is, isset can take the full nested variable, and it checks in step. You do not need to clal three issets in a row on each level of your intended value.
if (!empty($result->data['id'])) {
echo $result->data['id'];
}