AppDividend
Latest Code Tutorials

PHP strcmp() Example | strcmp() Function In PHP Tutorial

0

PHP strcmp() Example | strcmp() Function In PHP Tutorial is today’s topic. The strcmp() function compares two strings. The strcmp() function is binary-safe and case-sensitive. The strcmp() is an inbuilt function in PHP and 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.

PHP strcmp() Example

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, then 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 equals to the second string.

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 to be used in the comparison.
  2. string2 (mandatory): This parameter refers to the second string to be used in the comparison.

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

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

See the following example.

<?php

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

See the output.

➜  pro php app.php
0                                                                               
➜  pro

Now, let’s pass two different strings.

<?php

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

See the output.

➜  pro php app.php
-8                                                                              
➜  pro

Now, let’s take an example in which string1 is greater than string2.

<?php

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

See the output.

➜  pro php app.php
8                                                                               
➜  pro

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

== operator

The most common way you will see of comparing two strings is simply by using the == operator if the two strings are equal to each other then it returns true.

<?php

// 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 that 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 in uppercase it will still return false.

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

PHP compare two strings and return the difference

We can use the similar_text() function which calculates the similarity between two strings.

It calculates the similarity between two strings as 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 which 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.

<?php

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

See the following output.

➜  pro php app.php
1                                                                               
➜  pro

Finally, PHP strcmp() Example | strcmp() Function In PHP Tutorial is over.

Recommended Posts

PHP str_split Function Example

PHP str_repeat() Function Example

 Object In PHP Tutorial

Basics Of Class In PHP Tutorial

Leave A Reply

Your email address will not be published.

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