Backend Development 2 min read

PHP strcmp() Function: Usage, Parameters, Return Values, and Examples

This article explains the PHP strcmp() function, describing its binary‑safe, case‑sensitive string comparison behavior, detailing its parameters and return values, and providing multiple code examples that illustrate how different string inputs affect the function’s output.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP strcmp() Function: Usage, Parameters, Return Values, and Examples

The PHP strcmp() function compares two strings in a binary‑safe, case‑sensitive manner and returns an integer indicating their lexical relationship.

Parameters

string $str1 : The first string.

string $str2 : The second string.

Return values

Less than 0 if $str1 is less than $str2 .

Greater than 0 if $str1 is greater than $str2 .

0 if the strings are equal.

Example 1

";
echo strcmp("Hello", "hELLo");
?>

Output:

// 0
// < 0 (because "Hello" is less than "hELLo" in binary comparison)

Example 2

$str2
?>

Output:

// 0
// positive integer (e.g., 7)
Backend DevelopmentPHPCode Examplestring comparisonstrcmp
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.