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

<?php
echo strcmp("Hello", "Hello");
echo "<br>";
echo strcmp("Hello", "hELLo");
?>

Output:

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

Example 2

<?php
echo strcmp("Hello world!", "Hello world!"); // strings are equal

echo strcmp("Hello world!", "Hello"); // $str1 > $str2
?>

Output:

// 0
// positive integer (e.g., 7)
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

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

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.