[ Content | View menu ]

PHP Insanity X

Mark Mzyk | May 16, 2008

I’ll keep this insanity short and sweet.

In PHP, if you have a define that you forget to actually define, it will default to true, not false.

Why is this?  Because in evaluating the define, if PHP cannot find anything else to evaluate it to, it will evaluate it to a string literal, which always evaluates to true.

This behavior necessitates that all defines be strictly checked by type equality (===) or else your code might go down the opposite path you intended.  Or you can just make sure you always define any defines.  But you’re human like me, and mistakes will happen, so this behavior will likely affect you.

It’d be nice if PHP could reverse this behavior.  If a define isn’t defined, it’s false.  That seems like the logical thing to do.