The best way to test a boolean in PHP…
…or, at least, the best way IMO, is using the little known (to me) filter FILTER_VALIDATE_BOOLEAN present from version 5 of PHP.
Usage:
filter_var($your-boolean, FILTER_VALIDATE_BOOLEAN)
It’s pretty powerful especially when it’s the user that who determines and submits their values meaning true or false.
Possible return values are:
- TRUE for “1″, “true”, “on” and “yes”
- FALSE for “0″, “false”, “off” and “no”
- NULL otherwise.
Also, for those who develop plugins in wordpress, it’s ideal for shortcodes as it seems it does not make any difference whether the attribute value is written in upper or lower cases or enclosed in double, single quotes or not.
Wonder if these nerdy posts are related to my job, well, the answer is no. As a good friend once told me, "one never heals from programming!". It must be true in my case....
TrackBack
TrackBack URL for this entry:
http://www.stefaniamarchisio.com/2012/02/the-best-way-to-test-a-boolean-in-php/trackback/