While pair programming last week a colleague of mine showed me a great trick for validating user input with multiple options. This trick works best in PHP because of its fantastic array support. But with the right utility classes or frameworks you could really use it with any language. It just might not save you as much time as it does in PHP.

In this case we were writing some setters for a class. The input had two valid values “allow” and “deny”. The normal way I would do this is with an OR inside an if statement. Maybe you’d want to convert it to lowercase before the if block.

if($input == "allow" || $input == "deny")

So this works fine, and in this situation there will probably not be any other inputs that would need to be checked. Fast forward to another setter. This one can take 7 values:

if($input == "always" || $input == "hourly" || $input == "daily" || $input == "weekly" || $input == "monthly" || $input == "yearly" || $input == "never")

Ok, now I’m wishing there was an easier way to do this. Enter my colleague’s amazing tip:

To be honest I was embarrassed it hadn’t occurred to me before now. Both strtolower and in_array are VERY cheap operations in PHP, so using this to validate inputs with multiple acceptable values is a no brainer. As illustrated above I usually check the inverse of the if statement and throw an error or exception.