Re: Variable Fun

From: Date: Thu, 30 Jan 2003 22:26:58 +0000
Subject: Re: Variable Fun
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-133704@lists.php.net to get a copy of this message
On Friday 31 January 2003 05:55, James E Hicks III wrote: > This code: > <? > $var1 = "020610000030012452"; > $var2 = "020610000030012451"; > $test1 = (string) $var1 > $test2 = (string) $var2; > echo gettype($test2)."---".gettype($test1)."<BR><BR>"; > echo $test2."---".$test1."<BR><BR>"; > if ( $test2 != $test1){ > echo ("The variables are not equal."); > } > ?> > Produces the following output (Notice that it doesn't say vars are not > equal); > -------------------------------------------------------------------- > string---string > > 020610000030012451---020610000030012452 > > -------------------------------------------------------------------- > > And this code: > <? > $var1 = "A"."020610000030012452"; > $var2 = "A"."020610000030012451"; > $test1 = (string) $var1 > $test2 = (string) $var2; > echo gettype($test2)."---".gettype($test1)."<BR><BR>"; > echo $test2."---".$test1."<BR><BR>"; > if ( $test2 != $test1){ > echo ("The variables are not equal."); > } > ?> > Produces the following output (Notice it printed the message about vars not > being equal); > -------------------------------------------------------------------- > string---string > > 020610000030012451---020610000030012452 > > The variables are not equal. > -------------------------------------------------------------------- > > Why does PHP Version 4.2.3 ignore my (string) - cast to string in the if > evaluation? > I even tried putting the (string) inside the IF statement with no > difference in execution. > > > P.S. > The variables were reporting string before I even tried (string). String comparisons are best done using strcmp() or: if ($string1 === $string2) { echo "equal"; } or if ($string1 !== $string2) { echo "not equal"; } -- Jason Wong -> Gremlins Associates -> www.gremlins.biz Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* Code like that would not pass through anybody's yuck-o-meter. - Linus Torvalds about design on linux-kernel */

« previous php.general (#133704) next »