Deep Tech Point
first stop in your tech adventure
Home / PHP
strcmp or == in PHP
February 4, 2021 | PHP

This article is going to focus on the comparison operator known as Equal Operator and presented as “==” or double equal sign, and the inbuilt strcmp() function. We are going to look at the differences and similarities between these two.

What is the Equal Operator (==)?

Double equal sign or equal operator belongs to the group of comparison operators. As their name implies, comparison operators allow you to compare two values. In the case of equal operator (==), this operator accepts and compares two inputs and returns true, if both of the values are the same, or the operator returns a false value if both of the values are not the same. It is essential to bring out that the equal operator compares only the value of a variable and not data types.

$foo == $bar Returns true if $foo is equal to $bar

If you were to compare the data types too, you should be using the Identical operator (“===”), which returns true if $foo is equal to $bar – if both of the values are equal and the data types are equal too.

In addition to comparison operators – Equal and Identical operator – which test for equality and depending on comparison results return true or false value, you should be aware of the assignment operator (“=”), which assigns the variable on the left to have a new value
of the expression on the right.

In the above example, the PHP script will produce the output message “Strings are equal”.

What should we know about strcmp() function in PHP?

The binary-safe strcmp() function compares two strings. It is important to notice that the strcmp() function is case-sensitive, meaning the function differentiates between capital and lower-case letters. Luckily, strcasecmp() function enables a binary-safe case-insensitive string. However, if you rely on strcmp for safe string comparisons, both parameters must be strings, the result is otherwise extremely unpredictable.

Another comparison worth mentioning is a similarity with strncmp() function – with the with strncmp() function the difference is that you can specify the number of characters from each string to be used in the comparison.

Syntax:
strcmp( $string1, $string2 )
Parameters: This function accepts two parameters as mentioned above and described below:

$string1: This parameter refers to the first string to be used in the comparison. It is mandatory parameter.
$string2: This parameter refers to the second string to be used in the comparison. It is mandatory parameter.

Return Values: The function returns a random integer value depending on the condition of match, which is given by:

Returns 0 if the strings are equal.
Returns a negative value (< 0), if $string2 is greater than $string1. Returns a positive value (> 0) if $string1 is greater than $string2.

In the above example, the PHP script will produce the output message “Strings are not equal”.

In the end, equal operator (==) is probably faster than the strcmp function. However, the function is more descriptive in the code.

Sources:

http://php.net/manual/en/language.operators.comparison.php
http://php.net/manual/en/function.strcmp.php
https://www.w3schools.com/php/php_operators.asp
https://www.php.net/manual/en/function.strcmp.php