PHP strcmp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, strcmp() compares two strings and tells you which one is smaller, or if they are equal.

The comparison is case-sensitive and works byte by byte.

Syntax

Call strcmp() with the values you need. Start with this line.

PHP
$result = strcmp($a, $b);
CallWhat it does
strcmp("apple", "apple")Returns 0 (equal).
strcmp("apple", "banana")Returns a negative number.

Since PHP 8.2 the result is always -1, 0, or 1. In older versions it may be any negative or positive number.

Library declarations
PHP
strcmp(string $string1, string $string2): int

Parameters

ParameterDescription
string1Required. The first string.
string2Required. The second string.

Return Value

Return ValueDescription
intLess than 0 if string1 is less than string2, 0 if equal, greater than 0 if string1 is greater.

PHP Examples

Example 1 — Check If Two Strings Are Equal

A result of 0 means the strings match exactly.

PHP
<?php
if (strcmp("apple", "apple") === 0) {
    echo "Strings are equal", PHP_EOL;
}

How It Works

  1. Pass two strings. Both are "apple".
  2. Compare byte by byte. Every byte matches.
  3. Get 0. That is the code for equal.
  4. Test with === 0. A strict check is the safe habit.

Example 2 — Uppercase Comes Before Lowercase

Strings are compared by byte value, so case matters.

PHP
<?php
if (strcmp("Hello", "hello") < 0) {
    echo "Hello comes before hello", PHP_EOL;
} else {
    echo "hello comes first", PHP_EOL;
}

How It Works

  1. Compare H and h. They are different bytes.
  2. Check the codes. H is 72 and h is 104.
  3. Get a negative result. The first string is smaller.
  4. Use strcasecmp() when needed. It would treat them as equal.

Example 3 — Sort Words with usort()

Pass strcmp as the comparison function.

PHP
<?php
$fruits = ["pear", "apple", "fig"];

usort($fruits, "strcmp");

echo implode(", ", $fruits), PHP_EOL;

How It Works

  1. Start with an unsorted list. Words are in random order.
  2. Pass "strcmp" to usort(). It decides which word goes first.
  3. Words are ordered alphabetically. Case is respected.
  4. Join and print. implode() makes one line.

Frequently Asked Questions

strcmp() compares two strings byte by byte and returns a number telling you whether the first is smaller, equal to, or larger than the second.
strcmp(string $string1, string $string2): int. Pass the two strings to compare.
Yes. "Hello" and "hello" are not equal. Use strcasecmp() for a case-insensitive comparison.
It returns a negative number, 0, or a positive number. Since PHP 8.2 the values are always -1, 0, or 1.
Use === to check if two strings are equal. Use strcmp() when you need to know the order, such as in sorting.

Next: strcoll()

strcoll() compares two strings using the current locale's sorting rules.

strcoll() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful