Hello Friends Today, through this tutorial, I will tell you How do you check if two strings are equal using PHP With Example? In PHP, you can check if two strings are equal using the comparison operator `==` or the strict comparison operator `===`. Here’s how you can do it:
Using the `==` operator for loose comparison:
<?php $string1 = "Hello"; $string2 = "hello"; if ($string1 == $string2) { echo "The strings are equal."; } else { echo "The strings are not equal."; } ?>
This will output:
The strings are not equal.
Using the `===` operator for strict comparison (checks both value and data type):
<?php $string1 = "Hello"; $string2 = "hello"; if ($string1 === $string2) { echo "The strings are equal."; } else { echo "The strings are not equal."; } ?>
This will also output:
The strings are not equal.
In both cases, the strings are considered not equal because `==` and `===` comparison operators are case-sensitive. If you want to perform a case-insensitive comparison, you can convert both strings to the same case using `strtolower()` or `strtoupper()` function before comparison.