PHP strcasecmp() Function: Syntax, Parameters, Return Values, and Usage Examples

The article explains PHP's case‑insensitive string comparison function strcasecmp(), covering its syntax, parameters, return values, practical code examples, and why it is useful for handling textual data without regard to letter case.

php Courses
php Courses
php Courses
PHP strcasecmp() Function: Syntax, Parameters, Return Values, and Usage Examples
strcasecmp()

is a PHP string function that compares two strings without considering case.

Syntax of strcasecmp() function

strcasecmp(string $str1, string $str2): int

Parameter description:

$str1

: the first string to compare. $str2: the second string to compare.

Return value:

If $str1 and $str2 are identical, the function returns 0.

If $str1 is greater than $str2, it returns a value greater than 0.

If $str1 is less than $str2, it returns a value less than 0.

Usage of strcasecmp() function

<?php
$string1 = "Hello";
$string2 = "hello";
$result = strcasecmp($string1, $string2);
echo $result; // outputs 0 because characters are the same ignoring case

$string3 = "Hello";
$string4 = "HELLO";
$result = strcasecmp($string3, $string4);
echo $result; // outputs 0 because strings are identical

$string5 = "world";
$string6 = "World";
$result = strcasecmp($string5, $string6);
echo $result; // outputs a value less than 0 because $string5 is less than $string6
?>

Summary

Using strcasecmp() is advantageous because it ignores case differences, which is useful for comparing and sorting textual data, such as matching user input regardless of case.

PHP learning recommendations:

Vue3+Laravel8+Uniapp Beginner to Real‑World Development Tutorial

Vue3+TP6+API Social E‑commerce System Development Course

Swoole From Beginner to Master Recommended Course

Workerman+TP6 Real‑time Chat System Limited‑time Offer

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPstring-comparisoncase-insensitivestrcasecmp
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.