August 17, 2024
PHP String Compare
Comparison of two strings in PHP is very easy task. Like integer values you can also compare string with using equal to operator (==) inside of if conditional statement. It is as easy as you are comparing two values. The example of string comparison is mentioned below:
Code
<?php
function compare_two_string($str1, $str2){
if($str1 == $str2) {
print("Strings are equal");
return true;
} else {
print("Strings are not equal");
return false;
}
}
echo compare_two_string( "txtstr1","txtstr2");
?>
In above mentioned example we have created a function with the name of 'compare_two_string' which required two string parameters for comparison. We called this function in above mentioned script for comparing two strings. compare_two_string function compares two strings if strings are found equal then it shows an message strings are equal and returns true value. And if strings are not equal then it returns another message that 'String are not equal.' and returns false.