strcmp() – PHP String Functions

Syntax :

strcmp ( string1, string2 );

Description :

strcmp() function will compare two strings. This is case-sensitive function.

Note : Binary safe and case-sensitive function.


 Parameter :

  • string1 – This is a Required parameter. It is the first string which will be compared.
  • string2 – This is a Required parameter. It is the second string which will be compared.

Output :

Return values in this function are:

  • 0 – if the two strings are equal.
  • < 0 – if string1 is less than string2.
  • > 0 – if string1 is greater than string2.

Related articles : strcasecmp(), substr_compare(), strstr(), substr().


strcmp() – PHP Functions Example 1 : It returns 0, if the two strings are equal.
<?php
echo strcmp("Hi from tutorialmines.","Hi from tutorialmines."); // case-sensitive comparison
?>

Output of above code in the browser is as below:

0

strcmp() – PHP Functions Example 2 : It returns 0, if the two strings are equal. Case of character matter in this function. “Hi from tutorialmines.” is different from “hI from Tutorialmines.”.
<?php
echo strcmp("Hi from tutorialmines.","Hi from tutorialmines.");
echo "<br/>";
echo strcmp("Hi from tutorialmines.","hI from Tutorialmines.");
?>

Output of above code in the browser is as below:

0
-32

strcmp() – PHP Functions Example 3 :
<?php
echo "String1 is equal to string2<br/>";
echo strcmp("Hi from tutorialmines.","Hi from tutorialmines.");
echo "<br/>String1 is greater than string2<br/>";
echo strcmp("Hi from tutorialmines.","Hi from.");
echo "<br/>String1 is less than string2<br/>";
echo strcmp("Hi from tutorialmines.","Hi from tutorialmines from PHP section.");
?>

 


You may also like...

Leave a Reply

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