Thursday, December 15. 2005comparing strings in PHP with the == operatorComments
Display comments as
(Linear | Threaded)
As you noted, this example returns TRUE:
var_dump('01' == 1); You'd think that's reasonable, but the real inconsistency is shown by this example: var_dump('010' == 010); This returns FALSE. The former evaluates to decimal, and the latter to octal.
I think the comparison of '01' == 1 should be exact, the existing comparison should be done by something like '01' eq 1
I also believe that new users are confused about it, it is not easier and not pedagogically useful.
I want to add a strange example:
foreach( $MyArray as $elem ) { if ( $elem == 'isNotEmpty' ) continue; //do work... ...... } if the array if filled with ( 0, 1, 'isNotEmpty' ) only the 1 is not trapped by the if sentences, so 0 == 'isNotEmpty'. Placing the === operator fix the problem but the reason remain a mystery. |
Calendar
CategoriesPopular EntriesSetting up your own PEAR channel with Chiara_PEAR_Server - the official way
(36) Do you develop a website? It is infinitely better to synchronize live and development sites using the PEAR Installer(25) How to put the FAIL in open source(22) doing the PEAR thing(19) Using PEAR 1.4.0 to install PEAR packages on a remote host(19) phpDocumentor and __get/__set/__call - give us your ideas (RFC)(17) PEAR now fits in a bottle: meet go-pear.phar(17) Mac OS X ships with security hole-laden PEAR - how to upgrade immediately(16) Introducing pecl extension phar(13) go-pear.phar works! In related news, PHP_Archive is now PHP 5.1.0+(12) |
|||||||||||||||||||||||||||||||||||||||||||||||||
由于PHP是一种弱类型的语言,也就是说数据类型这个概念在PHP中被弱化。因而如果在编程时过分忽略数据类型(也是大部份PHP程序员的通病),会产生一些问题,甚至导致安全漏洞……
Tracked: Dec 19, 05:17
Me encontre con este post y me parecio que le podia llegar a ahorrar a alguien horas de dolores de cabeza. En resumen es una comparacion en la que PHP no compara como uno esperaria, y tendriamos que usar “===” (forzar chequeo de tipo de da...
Tracked: Dec 19, 12:32