How to Compare Two Strings in PHP?
Hi Dev,
If you need to see example of How to Compare Two Strings in PHP. This article goes in detailed on PHP strcasecmp() Function. you can understand a concept of Case insensitive string comparison using php. I explained simply step by step php compare strings case insensitive Code Example. So, let's follow few step to create example of Comparing Strings with strcasecmp function in PHP.
You can use the PHP strcmp() function for compare two strings. This function is similar to strncasecmp(), Use the PHP strcasecmp() function for compare two strings ( case insensitive ).
Example 1
index.php
<?php
$str1 = "Hello";
$str2 = "Hello World";
echo strcmp($str1, $str2);
?>
Output:
-6
Example 2
index.php
<?php
echo strcasecmp("Hello","HELLO");
echo "<br>";
echo strcasecmp("Hello","hELLo");
?>
Output:
0
0
Example 3
index.php
<?php
// PHP program to demonstrate how to compare two strings (case sensitive)
$str1 = "I love PHP";
$str2 = "I Love";
// compare strings in PHP with strcasecmp
$str=strcasecmp($str1, $str2);
echo "$str";
?>
Output:
4
I hope it could help you...
- How To Use Php Conditional Statement Example
- How to Check if String Contains Regex in PHP?
- PHP Array_column() Function Example
- PHP Generate List of Random Numbers Between 0 and 100 Example
- How to User Login with Facebook in PHP?
- How to Explode Every Character into Array Using PHP Example?
- PHP Form Validation Tutorial
- PHP Convert XML to JSON Example