comparing string in PHP by strcmp() function

I am explaining you, how to compare two string in PHP by using the strcmp() function. it is case-sensitive function.


strcmp() function will take two string as parameter string1 and string2. if function will return < 0 then the first string is less than the second string, if it will return > 0 then the first string is greater than a second string and if it will return 0 then both strings are equal.

Syntax :

strcmp(string1,string2)

string1 : it is required. it specifies the first string for comparison.
string2 : it is required. it specifies the second string for comparison.

Example :

<?php
echo strcmp("Hello Webdeveloperindia!","Hello Webdeveloperindia!"); //here both strings are equal
echo "<br>";
echo strcmp("Hello Webdeveloperindia!","Hello"); //here string1 is greater than string2
echo "<br>";
echo strcmp("Hello","Hello Webdeveloperindia India! "); //here string1 is less than string2 
?>

OutPut :

0
19
-26

Leave a Reply

Your email address will not be published. Required fields are marked *

− 7 = 2