strcasecmp() returns 0 when two strings are equal ignoring ASCII case, a negative value when the first sorts before the second, and a positive value when it sorts after.
if (strcasecmp('PHP', 'php') === 0) {
echo 'Same text ignoring case';}echo strcasecmp("Hello world","HELLO WORLD"); // Output is 0
This performs a binary-safe comparison ignoring ASCII letter case using strcasecmp function.$str1 = "Hello World";$str2 = "hello world";$result = strcasecmp($str1, $str2); // 0
if ($result === 0) {
echo "Both strings are matching";} else {
echo "Both strings are different";}
Output is
Both strings are matching
Example 2
$str1 = "Hello";$str2 = "hello world";$result = strcasecmp($str1, $str2);if ($result < 0) {
echo "First string sorts before the second" . PHP_EOL;}if ($result !== 0) {
echo "Both strings are different";}
Output is
First string sorts before the second
Both strings are different
Example 3
$str1 = "Hello World";$str2 = "hello";$result = strcasecmp($str1, $str2);if ($result > 0) {
echo "First string sorts after the second" . PHP_EOL;}if ($result !== 0) {
echo "Both strings are different";}
First string sorts after the second
Both strings are different
$str1 = 'Apple';$str2 = 'apple';var_dump(strcmp($str1, $str2) !== 0); // true (case-sensitive)
var_dump(strcasecmp($str1, $str2) === 0); // true (ignores ASCII case)
$str1 = '';$str2 = 'test';var_dump(strcmp($str1, $str2) < 0); // true
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.