How to Compare Two Strings in PHP?

Nov 02, 2022

.

Admin

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...

#PHP