PHP Program to Check if Two Strings are Same or Not
Last Updated :
20 Mar, 2024
Given two strings, the task is to check whether two strings are same or not in PHP. In this article, we will be looking at different approaches to check two strings are same or not.
Examples:
Input: str1 = "Geeks", str2 = "Geeks"
Output: Both Strings are the same
Input: str1 = "Hello", str2 = "Geeks"
Output: Both Strings are not same
Below are the approaches to check if two strings are same or not using PHP:
Using Comparison '=' Operator
The simplest way to check if two strings are the same in PHP is by using the equality comparison operator (==). This operator compares the values of two strings and returns true if they are equal, and false otherwise.
Note: We can also use strict comparison operator (===) to perform the same operation.
Example: This example shows the implementation of the above-explained approach.
PHP
<?php
$str1 = "Geeks";
$str2 = "Geeks";
if ($str1 == $str2) {
echo "Both strings are the same.";
} else {
echo "Both strings are not the same.";
}
?>
OutputBoth strings are the same.
Using strcmp( ) Function
The strcmp( ) function compares two strings. It is case-sensitive and returns 0 if the strings are equal, a negative number if the first string is smaller than the second, and a positive number if the first string is greater than the second.
Example: This example shows the implementation of the above-explained approach.
PHP
<?php
$str1 = "Hello";
$str2 = "Geeks";
if (strcmp($str1, $str2) == 0) {
echo "Both strings are the same.";
} else {
echo "Both strings are not the same.";
}
?>
OutputBoth strings are not the same.
Using strcasecmp( ) Function
The strcasecmp( ) function compares two strings in a case-insensitive manner. This function works similarly to strcmp( ) but does not consider the case(lower or upper) of the characters.
Example: This example shows the implementation of the above-explained approach.
PHP
<?php
$str1 = "Hello Geeks";
$str2 = "hello geek";
if (strcasecmp($str1, $str2) == 0) {
echo "Both strings are the same (case-insensitive).";
} else {
echo "Both strings are not the same (case-insensitive).";
}
?>
OutputBoth strings are not the same (case-insensitive).
Similar Reads
PHP Program to check a string is a rotation of another string Given the two strings we have to check if one string is a rotation of another string. Examples: Input : $string1 = "WayToCrack", $string2 = "CrackWayTo"; Output : Yes Input : $string1 = "WillPower" $string2 = "lliW"; Output : No. The above problem can be easily solved in other languages by concatena
3 min read
Check if two strings are permutation of each other Write a function to check whether two given strings are Permutation of each other or not. A Permutation of a string is another string that contains same characters, only the order of characters can be different. For example, "abcd" and "dabc" are Permutation of each other. We strongly recommend that
13 min read
PHP to check substring in a string In this article, we will see how to check the substring in a string. We are given two strings & we have to check whether the second string is a substring of the first string or not using PHP built-in strpos() function. This function is case-sensitive, which means that it treats upper-case and lo
2 min read
PHP program to check for Anagram An anagram is a word or phrase formed by rearranging the letters of another word or phrase, using all the original letters exactly once. To check for anagrams in PHP, remove spaces, convert to lowercase, sort characters, and compare the sorted strings or arrays.ExamplesInput : "anagram", "nagaram"Ou
5 min read
Check if a given String is Binary String or Not using PHP Given a String, the task is to check whether the given string is a binary string or not in PHP. A binary string is a string that should only contain the '0' and '1' characters. Examples:Input: str = "10111001"Output: The string is binary.Input: str = "123456"Output: The string is not binary.Table of
5 min read
How to check if a String Contains a Substring in PHP ? Checking whether a string contains a specific substring is a common task in PHP. Whether you're parsing user input, filtering content, or building search functionality, substring checking plays a crucial role.MethodsBelow are the following methods by which we can check if a string contains a substri
1 min read