Backend Development 2 min read

Binary‑Safe String Comparison with strcmp() in PHP

This article explains PHP's binary‑safe strcmp() function, detailing its case‑sensitive behavior, parameter meanings, return values, and provides a complete example demonstrating how to compare two strings and handle the result in code.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Binary‑Safe String Comparison with strcmp() in PHP

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

Note: The comparison distinguishes between uppercase and lowercase characters.

Function signature

int strcmp(string $str1, string $str2)

Parameters

str1 : The first string to compare.

str2 : The second string to compare.

Return values

If str1 is less than str2 , returns a value less than 0.

If str1 is greater than str2 , returns a value greater than 0.

If both strings are equal, returns 0.

Example usage

<?php
$var1 = "Hello";
$var2 = "hello";
if (strcmp($var1, $var2) !== 0) {
    echo "$var1 is not equal to $var2 in a case sensitive string comparison";
} else {
    echo "equal";
}
// Output: $var1 is not equal to $var2 in a case sensitive string comparison
backendPHPtutorialstring 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.