String comparison

String comparing in PHP is one of the most important part of our projects.

Comparing strings seems like an easy task but you will have to remember some key differences when using comparison operators == (equal operator), >= (greater than or equal to), <= (less than or equal to), <> (not equal), > (greater than), < (less than)etc. These operators implicitly convert the type of a string.

See example:

<?php

    $a = 2; //a PHP integer

    $b = '2'; //a PHP string

    var_dump($a == $b); //results bool(true)

    var_dump($a <> $b); //results bool(false)

    var_dump($a >= $b); //results bool(true)

    var_dump($a <= $b); //results bool(true)

?>

PHP is a loosely typed language and automatically converts the variable to the correct data type, depending on its value. In above example, we compared an integer value $a with a string value $b, PHP automatically converts the string value $b into the integer value. Always remember when comparing an integer with a string, the string is converted to a number. And if you compare two numerical strings, they are compared as integers. See example:

<?php

    $a = 2; //a PHP integer

    $b = '2PHP'; //a PHP string

    var_dump($a == $b); //results bool(true)

    var_dump($a <> $b); //results bool(false)

    var_dump($a >= $b); //results bool(true)

    var_dump($a <= $b); //results bool(true)

?>

Still getting the same results... strange? Didn't you expecting the false result of $a==$b? Always remember, when comparing strings, you should use === identical operator, === also called type checking operator. Identical operator === compares the values, types and return true if both values and types are same.

<?php

    $a = 2; //a PHP integer

    $b = '2PHP'; //a PHP string

    var_dump($a === $b); //results bool(false)

    var_dump($a !== $b); //results bool(true)

?>

strcmp() - string comparison

PHP strcmp() function: PHP offer a bit more than just comparing strings and returning true or false. The strcmp() function compares two strings.

Syntax

strcmp($str1, $str2)

Return values

The strcmp() returns:

  • < 0 if str1 is less than str2
  • > 0 if str1 is greater than str2
  • 0 if str1 and str2 are equal

Examples

Example 1: strcmp() returns a positive value (> 0) when the string passed as the first parameter is greater than the second parameter, see example:

<?php

    $str1 = 'b';
    $str2 = 'a';
    echo strcmp($str1,$str2); //prints 1 (which is > 0)

?>

Example 2: strcmp() returns a negative value (< 0) when the string passed as the first parameter is smaller than the second parameter, see example:

<?php

    $str1 = 'a';
    $str2 = 'b';
    echo strcmp($str1,$str2); //prints -1 (which is < 0 )

?>

Example 3: If both strings are equal, strcmp() returns 0, see example:

<?php 

    $str1 = 'apache';
    $str2 = 'apache';
    echo strcmp($str1,$str2); //prints 0

?>

strcasecmp - case insensitive string comparison

strcmp() function is case sensitive. If no case sensitivity is required, then you can use strcasecmp(). It works as strcmp() but it does not distinguish between uppercase and lowercase letters.

strcasecmp() example

<?php

    $str1 = 'apache';
    $str2 = 'APACHE';
    echo strcasecmp($str1, $str2); //prints 0
?>

Above example outputs: 0