Strong typed or Weak typed? (was: if("NANC" < 0) - always evaluates TRUE...)

From: Date: Wed, 07 Jan 2004 00:13:37 +0000
Subject: Strong typed or Weak typed? (was: if("NANC" < 0) - always evaluates TRUE...)
Groups: php.general 
Request: Send a blank email to php-general+get-173926@lists.php.net to get a copy of this message
> -----Original Message----- > From: CPT John W. Holmes [mailto:holmes072000@charter.net] > > From: "Kelly Hallman" <khallman@ultrafancy.com> > > > On Tue, 6 Jan 2004, Ivo Pletikosic wrote: > > > $data = 'NANC'; > > > if(is_numeric($data) && $data < 0) { die('Not OK'); } > > > > Interesting problem, one of the first legit oddities I've seen since > > joining the list. Anyway, in addition to your workaround, > casting the > > variable as an int also appears to result in the desired behavior: > > > > (int)"NANC" < 0 == false > > This all kind of begs the question of why you'd check if a > "string" was less > than zero, anyhow, doesn't it??? > > ---John Holmes... I have a function that returns a string on success or a negative value on error. I was operating under the impression that php was weakly typed but the bug.php.net entry I just did was summarily closed with the comment "never ever compare different types like that". So I am incorrect about what weakly typed means or something is off. In php I indicate strong-type comparisons with === and weak with == . When doing relative (< or > ) comparisons tho then casting is necessary or the results are unexpected. C.

« previous php.general (#173926) next »