Stupid question – I’m surprised this one has bitten me. Why do undefined constants in PHP evaluate to true?
Test case:
<?php
if(WHATEVER_THIS_ISNT_DEFINED)
echo 'Huh?';
?>
The above example prints ‘Huh?’
Thanks so much for your help! 🙂
Try
defined('WHATEVER_THIS_ISNT_DEFINED')When PHP encounters a constant that is not defined, it throws an
E_NOTICE, and uses the constant name you’ve tried to use as a string. That’s why your snippet printsHuh!, because a non-empty string (which is not"0") will evaluate totrue.From the manual:
If you set your error reporting level to report
E_NOTICEs, which is a good practice during development, you will also see the notice thrown.