PHP String Compare Case Insensitive

In this tutorial, you will learn how to compare two case insensitive strings in PHP.

How to Compare Case Insensitive String in PHP?

Here are two ways:

  • Case-Insensitive String Comparison in PHP
  • Using Case-Insensitive Comparison in Sorting

Case-Insensitive String Comparison in PHP

In PHP, you can perform case-insensitive string comparison using the strcasecmp() or strnatcasecmp() function.

Here’s an example of using strcasecmp() to compare two strings case-insensitively:

$string1 = "Hello World";
$string2 = "hello world";

if (strcasecmp($string1, $string2) == 0) {
    echo "The two strings are equal.";
} else {
    echo "The two strings are not equal.";
}

Output:

The two strings are equal.

Now let’s take another example using strnatcasecmp():

$string1 = "Chapter 2: Introduction";
$string2 = "chapter 10: conclusion";

if (strnatcasecmp($string1, $string2) < 0) {
    echo "$string1 comes before $string2.";
} else {
    echo "$string1 comes after $string2.";
}

Output:

Chapter 2: Introduction comes before chapter 10: conclusion.

Using Case-Insensitive Comparison in Sorting

Case-insensitive string comparison is particularly useful when sorting arrays of strings, where case sensitivity can affect the ordering of the elements. In PHP, you can use the usort() function with a custom comparison function that uses strcasecmp() to perform case-insensitive sorting.

Here’s an example of sorting an array of strings case-insensitively:

$fruits = array("apple", "Orange", "banana", "kiwi", "pear");
usort($fruits, function($a, $b) {
    return strcasecmp($a, $b);
});

print_r($fruits);

Output:

Array
(
[0] => apple
[1] => banana
[2] => kiwi
[3] => Orange
[4] => pear
)

Conclusion

In summary, when comparing strings in PHP, it is important to consider case sensitivity. PHP provides two built-in functions strcasecmp() and strnatcasecmp() to perform case-insensitive string comparison.

Recommended PHP Tutorials

  1. Functions: Remove First Character From String PHP
  2. Remove Specific/Special Characters From String In PHP
  3. How to Replace First and Last Character From String PHP
  4. PHP Count Specific Characters in String
  5. Reverse String in PHP

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *