String comparison using == vs strcmp() in PHP
The comparison operator called Equal Operator is the double equal sign “==”. This operator accepts two inputs to compare and returns true value if both of the values are same (It compares only value of variable, not data types) and returns false value if both of the values are not same.
This should always be kept in mind that the present equality operator == is different from the assignment operator =. The assignment operator assigns the variable on the left to have a new value as the variable on right, while the equal operator == tests for equality and returns true or false as per the comparison results.
Example:
<?php // Declaration of strings $name1 = "Geeks"; $name2 = "Geeks"; // Use == operator if ($name1 == $name2) { echo 'Both strings are equal'; } else { echo 'Both strings are not equal'; } ?> |
chevron_right
filter_none
Output:
Both strings are equal
The strcmp() is an inbuilt function in PHP which is used to compare two strings. This function is case-sensitive which points that capital and small cases will be treated differently, during comparison. This function compares two strings and tells whether the first string is greater or smaller or equals to the second string.
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.
Example:
<?php // Declaration of strings $name1 = "Geeks"; $name2 = "geeks"; // Use strcmp() function if (strcmp($name1, $name2) !== 0) { echo 'Both strings are not equal'; } else { echo 'Both strings are equal'; } ?> |
chevron_right
filter_none
Output:
Both strings are not equal
Reference:
- http://php.net/manual/en/language.operators.comparison.php
- http://php.net/manual/en/function.strcmp.php
Recommended Posts:
- PHP | strcmp() Function
- Java 11 - Features and Comparison
- Comparison between static and instance method in PHP
- Comparison - Centralized, Decentralized and Distributed Systems
- How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
- Find the count of palindromic sub-string of a string in its sorted form
- Remove characters from a numeric string such that string becomes divisible by 8
- Convert an ArrayList of String to a String array in Java
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Number of sub-strings which are anagram of any sub-string of another string
- Check whether second string can be formed from characters of first string
- Check if a string contains a palindromic sub-string of even length
- PHP | Program to check a string is a rotation of another string
- Sub-strings of a string that are prefix of the same string
- PHP | Different characters in the given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



