i assume you can also use the ! (not) operator
<?php
if (!strncasecmp($string, 'Trudeau', 4)){
print "true";
}
?>
since booleans in PHP translate 0 to FALSE and any other integer to TRUE.
!0 = !false = true
!1 = !true = false
!-1 = !true = false
strncasecmp
(PHP 4 >= 4.0.2, PHP 5)
strncasecmp — Binárně bezpečné case-insensitive porovnání prvních n znaků řetězců
Popis
int strncasecmp ( string $str1, string $str2, int $len )Tato funkce se podobá strcasecmp(), s tím rozdílem, že můžete určit (maximální) počet znaků (len) z každého z řetězců, které se použijí při porovnání. Pokud je některý z řetězců kratší než len, pak se pro porovnání použije délka tohoto řetězce.
Pokud je str1 méně než str2, vrací < 0; pokud je str1 větší než str2, vrací > 0, a 0, pokud jsou stejné.
Viz také: ereg(), strcasecmp(), strcmp(), substr(), stristr() a strstr().
strncasecmp
khootz
28-Nov-2006 05:23
28-Nov-2006 05:23
djdykes
31-Aug-2005 12:43
31-Aug-2005 12:43
Hi all,
be aware when comparing strings using the strcmp family. if you write code like this...
if (strncasecmp($string, 'Trudeau', 4))
print "true";
The above code returns 1 which evaluates to boolean 'true'
that statement will always be true... because these functions return 0 only when equal. so a better test would be
if ( (strncasecmp($string, 'Trudeau', 4)) == 0)
print "true";
always test these functions equality with 0
regards
