PHP strcmp: How to Compare two strings in PHP

During any programming, you will always get situations where you need to compare values with each other, and if the values are boolean or integers, then the comparison is simple.

But if you want to compare strings or parts of strings, there can be more to the comparison, such as a case of the string you are comparing.

The function compares the two strings and tells us whether the first string is greater or smaller than the second string or equal to the second string.

PHP strcmp

PHP strcmp() is a built-in function that compares two strings. The strcmp() function is binary-safe and case-sensitive. The strcmp() function is used to compare two strings.

The strcmp() function is case-sensitive, which points out that capital and small cases will be handled differently during the comparison.


See the following syntax.

strcmp(string1, string2)


This function accepts two parameters, which are described below:

  1. string1 (mandatory): This parameter refers to the first string used in the comparison.
  2. string2 (required): This parameter refers to the second string used in the comparison.

Return Value

The function returns the random integer value depending on the condition of the match, which is given by:

  1. Returns 0 if the strings are equal.
  2. Returns a negative value (< 0) if string2 is greater than string1.
  3. Returns a positive value (> 0) if string1 is greater than string2.

See the following example.


echo strcmp("AppDividend", "AppDividend");

See the output.

➜  pro php app.php
➜  pro

Now, let’s pass two different strings.


echo strcmp("Eleven", "Millie Bobby Brown");

See the output.

➜  pro php app.php
➜  pro

Let’s take an example in which string1 is greater than string2.


echo strcmp("Millie Bobby Brown", "Maddison");

See the output.

➜  pro php app.php
➜  pro

So, this is how to compare two strings in PHP using the strcmp() function.

== operator

The most common way to compare two strings is simply using the == operator. If the two strings are equal to each other, then it returns true.


// Using the == operator, Strings match is printed
if('KRUNAL' == 'krunal')
    echo 'Strings match.';
} else {
    echo 'Strings do not match.';

See the output.

➜  pro php app.php
Strings do not match.                                                           
➜  pro

This code will return that strings were not in the same case. It will not match.

If all the letters in one string were in uppercase, then this will return false, and the strings do not match.

This means that we can’t use the == operator when comparing strings from user inputs; even if the first letter is uppercase, it will still return false.

So we need to use some other function to help compare the strings.

Comparing two strings and returning the difference

The similar_text() function calculates the similarity between two strings. It calculates the similarity between two strings described in Programming Classics: Implementing the World’s Best Algorithms by Oliver (ISBN 0-131-00413-1).

Note that this implementation does not use a stack as in Oliver’s pseudocode, but recursive calls may or may not speed up the whole process.

Note also that the complexity of this algorithm is O(N**3), where N is the length of the longest string.


See the following example.


$diff = similar_text('Krunal', 'Ankit', $perc);
echo $diff;

See the following output.

➜  pro php app.php
➜  pro

That’s it for this tutorial.

Recommended Posts

PHP str_split

PHP str_repeat

PHP Object

Class in PHP

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.