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(), за исключением того, что можно указать максимальное количество символов в обоих строках, которые будут участвовать в сравнении.
Возвращает отрицательное число, если str1 меньше, чем str2 ; положительное число, если str1 больше, чем str2 , и 0 если строки равны.
См. также описание функций ereg(), strcasecmp(), strcmp(), substr(), stristr() и strstr().
strncasecmp
khootz
27-Nov-2006 08:23
27-Nov-2006 08:23
djdykes
31-Aug-2005 03:43
31-Aug-2005 03: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
