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.


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....

Leave a Reply

Your email address will not be published. Required fields are marked *

− four = 1

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>