2011年4月26日星期二

Evert Pot's Blog: Numeric string comparison in PHP


In this new post to his blog Evert Pot reminds you (the PHP developer) about the loose and strict typing checks that you need to do when checking the values of your variables and never just assume. PHP's loose typing tendencies can cause issues if you're not paying attention.



As long as you make sure you always use strict checking (=== and !==) where you can, and fall back to the loose checks when you must. As a PHP developer, I think it's very important to understand and memorize exactly how these work, whether you're writing new code, or maintaining existing code.


He points out a specific example of some code that seems counter-intuitive when you compare how it reads and how it actually works (strings converted to floats and evaluated)



The moral is: always do strict checks when you are able to.

没有评论:

发表评论