Current Location: Home> Latest Articles> In-Depth Explanation and Usage of the strcmp() Function in PHP

In-Depth Explanation and Usage of the strcmp() Function in PHP

gitbox 2025-06-28

What is the strcmp() Function

strcmp() is one of the string functions in PHP, used to compare two strings. If they are identical, it returns 0; if the first string is smaller, it returns a negative number; otherwise, it returns a positive number.

Function Syntax

strcmp(string $str1, string $str2): int

Parameters

$str1: The first string to compare

$str2: The second string to compare

Return Value

strcmp() returns an integer based on the comparison of the two strings. If the strings are equal, it returns 0; if string 1 is smaller than string 2, it returns a value less than 0; if string 1 is greater than string 2, it returns a value greater than 0.

Examples

Strings are Equal

$str1 = "Hello World";

$str2 = "Hello World";

echo strcmp($str1, $str2);

Output: 0

Explanation: Since $str1 and $str2 are equal, strcmp() returns 0.

Strings are Not Equal

$str1 = "Hello World";

$str2 = "Hello World!";

echo strcmp($str1, $str2);

Output: -1

Explanation: Since $str1 is smaller than $str2, strcmp() returns a value less than 0.

Difference Between the Operators “==” and “===”

In PHP, in addition to using the strcmp() function to compare strings, you can also use the “==” and “===” operators. However, they have significant differences compared to strcmp().

Using the “==” Operator to Compare Strings

The “==” operator is used to compare the values of two variables. If they are equal, it returns true; otherwise, it returns false. However, when comparing two strings, it may produce unpredictable results.

$str1 = "123";

$str2 = " 123";

if($str1 == $str2) { echo "true"; } else { echo "false"; }

Output: true

Explanation: Even though $str1 and $str2 have different values, the “==” operator still considers them equal.

Using the “===” Operator to Compare Strings

The “===” operator compares both the value and type of two variables. If both the value and type are the same, it returns true; otherwise, it returns false. When comparing two strings using this operator, it works as expected.

$str1 = "123";

$str2 = " 123";

if($str1 === $str2) { echo "true"; } else { echo "false"; }

Output: false

Explanation: With the “===” operator, $str1 and $str2 are considered different types, so the result is false.

Usage Scenarios for the strcmp() Function

Compared to the “==” and “===” operators, the strcmp() function is the preferred method for comparing two strings. It is frequently used to check if two strings are equal, for example, in form validation:

$str1 = $_POST['password'];

$str2 = "qwerty";

if(strcmp($str1, $str2) === 0) { // Correct password } else { // Incorrect password }

This code compares the user-entered password with a predefined password. If they are equal, the password is correct; otherwise, it is incorrect. Since strcmp() performs an exact comparison, it ensures that passwords are not incorrectly accepted.

Conclusion

strcmp() is a very useful string function in PHP for comparing if two strings are equal. While using the “==” and “===” operators might be easier in some cases, using strcmp() ensures accurate comparison results when working with strings.