Create Account
Sign In

Will comparison of string "25" and integer 52 work in PHP?

To create and answer topics, you must to be logged.
Pages: 1
Author Message
phpdeveloper
20/06/2012 at 11:57:04
phpdeveloper
Member
Will comparison of string "25" and integer 52 work in PHP?

PHP Web Application Development | Hire PHP Website Developer
Elizabeth00
02/09/2014 at 14:56:53
Elizabeth00
Member
well it seems to me that at least a warning should be given when comparing an integer 0 to a non-integer value that evaluates to 0 when cast as an int unless an explicit type cast is used without using strict equals.

i can see why this behaviour is not a bug, because (int)'SOME STRING' == 0, but it's also has the potential to cause unexpected results/bugs unless you use strict equals everywhere.

in this case, i fixed the problem by using === instead of ==, but it took a bit of time to track down the error because there was no warning or anything. i think that it would be better to require someone to do:

if($value == (int)'SOME STRING')

OR

if($value === 'SOME STRING')

in order to avoid a warning being emmited if $value == 0.

For Innovative Apps visit: ATI-ERP expeditous business solutions..

prince123
12/11/2014 at 09:04:52
prince123
Member
ates to 0 when cast as an int unless an explicit type cast is used without using strict equals.

i can see why this behaviour is not a bug, because (int)'SOME STRING' == 0, but it's also has the potential to cause unexpected results/bugs unless you use strict equals everywhere.

_________
prince


Pages: 1