How to use strcmp( ) Function In PHP
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
$str1 = "Hello";
$str2 = "Geeks";
if (strcmp($str1, $str2) == 0) {
echo "Both strings are the same.";
} else {
echo "Both strings are not the same.";
}
?>
Output
Both strings are not the same.
PHP Program to Check if Two Strings are Same or Not
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 β=β Operator
- Using strcmp( ) Function
- Using strcasecmp( ) Function