0

Is there a better way, in PHP, to both check that this variable exists and if it does, check its value?

Right now, I'm doing a lot of this nonsense - checking first if something exists, and then checking what the value is. There must be an easier way, but I don't quite know how to do it.

  if (isset($podcast['contentAdvisoryRating'])) {
    if ($podcast['contentAdvisoryRating']=='Explicit') {
      echo '<B>EXPLICIT</B><BR>';
    }
  }
jamescridland
  • 714
  • 2
  • 5
  • 24
  • `if(isset($podcast['contentAdvisoryRating']) && $podcast['contentAdvisoryRating'] == 'Explicit') {`? – j08691 Nov 24 '18 at 04:32
  • As of PHP7 you can use the Null coalescing operator `??` e.g. `if (($podcast['contentAdvisoryRating'] ?? '') == 'Explicit')` – Nick Nov 24 '18 at 05:00

0 Answers0