PHP Program to Check if Two Strings are Same or Not Last Updated : 20 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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: Table of Content Using Comparison '=' OperatorUsing strcmp( ) FunctionUsing strcasecmp( ) FunctionUsing Comparison '=' OperatorThe 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( ) FunctionThe 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( ) FunctionThe 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). Comment More infoAdvertise with us Next Article PHP Program to Check if Two Strings are Same or Not A ashokjaiswal Follow Improve Article Tags : PHP 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 How to check a string starts/ends with a specific string in PHP ? In this article, we are going to learn how to check that a given string is starting or ending with a given specific string in PHP.We require the use of XAMPP, Netbeans to run this code.Input: "Geeks For Geeks"Starting with: "G"Output: "Geeks For Geeks Starts with String G"In PHP version 8+, there ar 2 min read Shell Script To Check the String For a Particular Condition Here, we are going to write a shell script that will echo (print) a suitable message if the accepted string does not meet a particular condition. Suppose we have given some certain conditions then our task is to print the message if the accepted string doesn't match the condition. Example 1: If the 3 min read How to check if a String contains a Specific Character in PHP ? In PHP, determining whether a string contains a specific character is a common task. Whether you're validating user input, parsing data, or performing text processing, PHP provides several methods to check for the presence of a particular character within a string efficiently. Here are some common a 2 min read How to check for empty string in PHP ? In this article, we will see how to check for empty string in PHP. String is a set of characters. A string is said to be empty, if it contains no characters. We can use empty() function to check whether a string is empty or not.here we have some common approachesTable of ContentUsing empty() functio 4 min read Like