You will always get a situation where you want to compare the values with each other whether with the integers or with the string comparison.
We can easily compare two strings with the help of comparison operator.If the two strings are equal to each other it returns true.
if('string1' == 'string1')
{
echo 'Strings match.';
} else {
echo 'Strings do not match.';
}
Another way to compare the strings is by using strcmp function that will return 0 if string match.
if(strcmp('string1', 'string1') == 0)
{
echo 'Strings match.';
} else {
echo 'Strings do not match.';
}
So we can use these above function when comparing strings that are input by user.
We can also compare case insensitive strings with the help of strcasecmp function.
if(strcasecmp('string1', 'string1') == 0)
{
echo 'Strings match.';
} else {
echo 'Strings do not match.';
}
0 Comment(s)