PHP strncmp Function: Binary‑Safe String Comparison
The article explains PHP's strncmp function, a binary‑safe, case‑sensitive string comparison that compares the first N characters of two strings, detailing its syntax, parameters, return values, and providing practical code examples with expected outputs.
Function: int strncmp(string $str1, string $str2, int $len) – binary‑safe comparison of the beginning of two strings up to a specified length.
Description: The function works like strcmp() but allows you to limit the number of characters compared. It is case‑sensitive.
Parameters:
$str1 : first string.
$str2 : second string.
$len : maximum number of characters to compare.
Return value: Returns <0 if $str1 is less than $str2 , >0 if greater, and 0 if they are equal within the specified length.
Example 1:
Output:
// 0Example 2:
";
echo strncmp("Hello", "hELLo", 6);
?>Output:
// 0
// -1Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.