當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP strcasecmp()用法及代碼示例

strcasecmp()函數是PHP中的內置函數,用於比較兩個給定的字符串。不區分大小寫。此函數類似於strncasecmp(),唯一的區別是strncasecmp()提供了用於指定每個字符串中用於比較的字符數的規定。

用法:

strcasecmp($string1, $string2)

參數:此函數接受上述語法中所示的兩個必需參數,並在下麵進行描述:


$string1,$string2:這些參數指定要比較的字符串。

返回值:
該函數根據以下條件返回整數:

  • 如果兩個字符串相等,則strcasecmp()返回0。
  • strcasecmp()返回
  • strcasecmp()返回> 0-如果string1大於string2

例子:

Input : $str1 = "Geeks for Geeks "
        $str2 = "Geeks for Geeks "
Output : 0

Input : $str1 = "Geeks for Geeks"
        $str2 = "Hello Geek!"
Output : -1

以下示例程序旨在說明PHP中的strcasecmp()函數:

程序1:當兩個字符串相同時:

<?php 
// PHP program to demonstrate the use 
// of strcasecmp() function 
  
$str1 = "Geeks for Geeks "; 
$str2 = "Geeks for Geeks "; 
   
// Both the strings are equal 
$test=strcasecmp($str1, $str2);  
   
echo "$test";  
   
?>

輸出:

0

程序2:當兩個字符串不相同時:

<?php 
// PHP program to demonstrate the use 
// of strcasecmp() function 
  
$str1 = "Geeks for Geeks"; 
$str2 = "Hello Geek!"; 
   
// Both the strings are not equal 
//  str1 < str2  
  
$test = strcasecmp($str1, $str2);  
   
echo "$test";  
   
?>

輸出:

-1

程序3:當兩個字符串不相同時:

<?php 
// PHP program to demonstrate the use 
// of strcasecmp() function 
$str1 = "Hello Geek!"; 
$str2 = "Geeks for Geeks"; 
  
   
// Both the strings are not equal 
//  str1 > str2  
$test = strcasecmp($str1, $str2);  
   
echo "$test";  
   
?>

輸出:

1

參考:
http://php.net/manual/en/function.strcasecmp.php



相關用法


注:本文由純淨天空篩選整理自ChetnaAgarwal大神的英文原創作品 PHP | strcasecmp() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。