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 — バイナリ対応で大文字小文字を区別しない文字列比較を、 最初の n 文字について行う
説明
int strncasecmp ( string $str1, string $str2, int $len )この関数は、strcasecmp() に似ていますが、各文 字列から比較する文字数(の上限)(len)を指定 できるという違いがあります。どちらかの文字列が lenより短い場合、その文字列の長さが比較時 に使用されます。
str1 が str2 より短 い場合に < 0 を返し、str1 が str2 より大きい場合に > 0、等しい場合に 0を返します。
preg_match()、 strcasecmp()、 strcmp()、 substr()、 stristr() および strstr() も参照ください。
strncasecmp
khootz
28-Nov-2006 01:23
28-Nov-2006 01:23
djdykes
31-Aug-2005 07:43
31-Aug-2005 07: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