String comparison using == vs strcmp() in PHP
Last Updated :
11 Jul, 2025
In this article, we will see the string comparison using the equal (==) operator & strcmp() Function in PHP, along with understanding their implementation through the example.
PHP == Operator: The comparison operator called Equal Operator is the double equal sign "==". This operator accepts two inputs to compare and returns a true value if both of the values are the same (It compares the only value of the variable, not data types) and returns a false value if both of the values are not the 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: This example describes the string comparison using the == operator.
PHP
<?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';
}
?>
Output:
Both the strings are equal
PHP strcmp() Function: The strcmp() is an inbuilt function in PHP that 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 the second string. This function is binary-safe string 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 a mandatory parameter.
- $string2: This parameter refers to the second string to be used in the comparison. It is a mandatory parameter.
Return Values: The function returns a random integer value depending on the condition of the 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: This example illustrates the string comparison using the strcmp() function.
PHP
<?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';
}
?>
Output:
Both strings are not equal
Reference:
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Similar Reads
How to Compare Two Strings without using strcmp() function in PHP ? Comparing two strings is a common operation in programming. In PHP, the strcmp() function is typically used to compare two strings. However, there may be situations where you need to compare strings without using this built-in function, such as for learning purposes or to meet specific constraints.
4 min read
How to Copy String in PHP ? Copying a string is a basic operation that all the programming languages offer. In this article, we will explore different approaches to copying a string in PHP, including using the assignment operator, the strval() function, the substr() function and by implementing the strcpy() function.Table of C
3 min read
How to prepend a string in PHP? We have given two strings and the task is to prepend a string str1 with another string str2 in PHP. There is no specific function to prepend a string in PHP. To do this task, we have the following operators in PHP:Below are the methods to prepend a string in PHPTable of ContentUsing Concatenation Op
2 min read
How to check if URL contain certain string using PHP? Given a URL and the task is to check the URL contains certain string or not. The URL are basically the strings. So in order to check the existence of certain strings, two approaches can be followed. The first approach is used to find the sub string matching in a string and second approach is to find
4 min read
How to check if URL contain certain string using PHP? Given a URL and the task is to check the URL contains certain string or not. The URL are basically the strings. So in order to check the existence of certain strings, two approaches can be followed. The first approach is used to find the sub string matching in a string and second approach is to find
4 min read
How to check if URL contain certain string using PHP? Given a URL and the task is to check the URL contains certain string or not. The URL are basically the strings. So in order to check the existence of certain strings, two approaches can be followed. The first approach is used to find the sub string matching in a string and second approach is to find
4 min read