0

I make a lot of SOAP calls to external services in PHP, and sometimes the XML I get back provides a field, sometimes it provides that field, but empty, and sometimes it just doesn't provide it at all. So every time I want to store a value, I have to wrap it like so:

$age = (isset($person['age']) && !empty($person['age'])) ? $person['age'] : NULL;

I thought I could simply make a function to do that, or call empty() alone, but as soon as I do that without the isset check first, I get a call to an undefined index when the XML I get back lacks the age index.

Is there a shorthand way of checking BOTH isset and empty? Or some function I can build myself which won't result in this warning (but still will give me any others, in case there are REAL problems)?

Bing
  • 3,071
  • 6
  • 42
  • 81
  • 3
    `!empty($person['age'])` should work. But `empty` will return `true` for `0` & `false` also. Are you sure the error is on same line? – Sougata Bose Jul 26 '16 at 05:50
  • 2
    http://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty – Kevin Jul 26 '16 at 05:50
  • My mistake; I thought `empty($person['age'])` was returning a warning when not set. Apparently I was mistaken! – Bing Jul 26 '16 at 05:53

0 Answers0