Strcmp() is case sensitive. So use it when you need a case sensitive comparison.
---John Holmes... > -----Original Message----- > From: Bas Jobsen [mailto:[EMAIL PROTECTED]] > Sent: Saturday, July 27, 2002 12:35 PM > To: PHP General > Subject: [PHP] string comparison > > Hello, > > When should i prefer strcmp() above == or !=? > I have test it out (see code below). > But a == comparison seems to be faster always. > First i thought is was cause, if(strcmp($1,$2)!=0), > needs two comparisons (strcmp and !=). But even > if($1!=$2) is faster then if(strcmp($1,$2)). > > Thanks, > > Bas Jobsen > > <? > > function getmicrotime(){ > list($usec, $sec) = explode(" ",microtime()); > return ((float)$usec + (float)$sec); > } > > > $t1='hello'; > $t2='hello'; > $t3='hallo'; > > ?> > 1000x:<? > $start=getmicrotime(); > for($a=0; $a<100000; $a++) if(strcmp($t1,$t3));//nonmatch > echo getmicrotime()-$start; > ?> > <br> > 1000x:<? > $start=getmicrotime(); > for($a=0; $a<100000; $a++) if($t1!=$t3);//nonmatch > echo getmicrotime()-$start; > ?> > > <br> > 1000x:<? > $start=getmicrotime(); > for($a=0; $a<100000; $a++) if(!strcmp($t1,$t3));//match > echo getmicrotime()-$start; > ?> > <br> > 1000x:<? > $start=getmicrotime(); > for($a=0; $a<100000; $a++) if($t1==$t3);//match > echo getmicrotime()-$start; > ?> > <br> > > 1000x:<? > $start=getmicrotime(); > for($a=0; $a<100000; $a++) if(strcmp($t1,$t2));//nonmatch > echo getmicrotime()-$start; > ?> > <br> > 1000x:<? > $start=getmicrotime(); > for($a=0; $a<100000; $a++) if($t1!=$t2);//nonmatch > echo getmicrotime()-$start; > ?> > > <br> > 1000x:<? > $start=getmicrotime(); > for($a=0; $a<100000; $a++) if(!strcmp($t1,$t2));//match > echo getmicrotime()-$start; > ?> > <br> > 1000x:<? > $start=getmicrotime(); > for($a=0; $a<100000; $a++) if($t1==$t2);//match > echo getmicrotime()-$start; > ?> > <br> > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php