Friday, August 10, 2012

Testing strings in PHP

Continuing from my last post on testing int's, I was curious about strings as well. Now strings are a bit easier to test in PHP than int's, so I'll skip some of the back story and get to the good stuff.

As far as I can tell, there are two valid methods for testing the validity of string variable: is_string($var) and (string)$var === $var. Now is_string is certainly easier to remember, but is it as fast?

The Testing:

I created a simple test strategy, create a set of both valid and invalid values, loop through them 100,000 times and see which takes the least amount of time. Here is my code: