I was asked a strange question today. “What’s return !! $row; do?” And I had never really been witness to the syntax of the double-not ‘!!’ because really, to me, it’s a bit strange and contradictory for the coding styles I’m used to. So I looked it up a bit more to see if it was actually faster than proper type casting with (bool)$row in php. Nobody had any real results. I ran a quick test and sure enough using !! is faster… for arrays. Other than that, the time is so close it’s insignificant.
Here’s the quick ‘n dirty results:
Time testing (bool) vs. !! conversions – 1,000,000 interations
converted array with (bool) done in 1.06559991837 seconds.
converted array with !! done in 0.534655094147 seconds.
converted ” with (bool) done in 0.572640895844 seconds.
converted ” with !! done in 0.523654937744 seconds.
converted (int) with (bool) done in 0.49324798584 seconds.
converted (int) with !! done in 0.5086581707 seconds.
converted object with (bool) done in 0.564023017883 seconds.
converted object with !! done in 0.556074857712 seconds.
I had to do one million iterations just so the times would add up to something substantial. Really, the difference is only faster if you happen to want to get a quick ‘true or false’ from an array. The rest are so insignificant it’s not worth the confusion of a double-not. Personally, I’d rather see an actual check of $row to see if it contains something useful before returning true or false just based on it being set. But that’s just me.