How to compare two strings in PHP?

How to compare two strings in PHP?

Asked on November 1, 2018 in PHP.
Add Comment


  • 2 Answer(s)

    Here the PHP strcmp() function is used.
         The PHP strcmp() function is used to compare two strings. This function takes two strings str1 and str2 as parameters. The strcmp() function returns < 0 if str1 is less than str2; returns > 0 if str1 is greater than str2, and 0 if they are equal.

    For example:

    <?php
    $str1 = "Hello";
    $str2 = "Hello World";
    echo strcmp($str1, $str2); // Outputs: -6
    ?>
    

    The PHP strcmp() function compare two strings in a case-sensitive manner. In case-insensitive comparison, strcasecmp() function is used.

     

    Answered on November 1, 2018.
    Add Comment

    eturns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

    === only returns true or false, it doesn’t tell you which is the “greater” string.

    Answered on February 25, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.