This article describes how to use the strncmp () function in PHP to compare the first two characters of two strings to see if they are equal. The example analyzes the functions of the strncmp () function, parameter usage, and usage tips, for more information about how to use the strncmp () function in PHP to compare the first two characters of two strings, see the example below. We will share this with you for your reference. The details are as follows:
The strncmp () function in PHP is used to compare two strings (case sensitive) to determine whether the first n characters of the two strings are equal.
The strncmp () function is defined as follows:
Strncmp (string1, string2, length)
Parameter description:
String1 is required. Specifies the first string to be compared.
String2 is required. Specifies the second string to be compared.
Length is required. Specifies the number of characters in each string used in the comparison.
Return value description:
If the two strings are equal, the return value is 0.
If string1 is less than string2, the return value is less than 0.
If string1 is greater than string2, the return value is greater than 0.
Sample codeAs follows:
<? The php/* strncmp () function compares the first n characters of two strings for equality. ** return value: 0-if the two strings are equal <0-if string1 is less than string2> 0-if string1 is greater than string2 */$ str1 = "welcome to www.jb51.net"; $ str2 = "welcome to www. JB51.NET "; $ str3 =" welcome to php "; echo strncmp ($ str1, $ str2, 2); echo"
"; Echo strncmp ($ str1, $ str3, 2);?>
The running result is as follows:
10