Search code examples
phpstrcmp

PHP strcmp not working


in my code I wanted to compare two strings and first I did it with === but later i tryed strcmp(). When i write:

echo strcmp("test","test");

the result was 0. I also tried it with ===

echo ($subject === $empty)

there i used my actual strings i wanted to compare.

However. Why is the string comparde method only True if the zweo string arent the same. Because:

echo strcmp("test","tedddst");

delivers True.


Solution

  • strcmp doesn't return a boolean true/false, it returns an int. From the strcmp documentation:

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