how to compare two strings in PHP if condition?

Question : How to compare two strings in PHP? :- PHP strcmp()
You can use the PHP strcmp() function to simply compare two strings. This Internal (built-in) functions takes two strings user_data_user_data_string1 and user_data_user_data_string2 as parameters. The strcmp() Internal (built-in) functions returns < 0 if user_data_user_data_string1 is less than user_data_user_data_string2; returns > 0 if user_data_user_data_string1 is greater than user_data_user_data_string2, and 0 if they are equal.

How To Compare Strings In PHP

define in any sort of devlop application programming you will always get condition where you required to compare data values with each other, if the data values are boolean like as true/false or integers then the any string user data comparison is easy. But if you want to compare any types of the data strings or some multiple parts of strings then there can be lots of the way to the comparison such as case of the string you are comparing. In this best example I am going to look at all the multiple ways you can compare strings in PHP using a number of built in PHP functions.

  • == operator
  • strcmp Function
  • strcasecmp Function

== operator

The most useful way you will getting output display like Your Strings match. or Your Strings do not match. of comparing two User data strings is simply by using the
PHP == operator
if the two data strings are equal to each other then it returns boolean data like as true.

Using the == operator, Strings match is printed

if('user_data_string1' == 'user_data_string1')
{
    echo 'Good Luck, User Strings match.';
} else {
    echo 'Sorry, Your User Strings do not match.';
}


This source code
will return that the strings match, but what if the Data strings were not in the same case it will not match. If all the letters in one string were in uppercase then this will return false and that the strings do not match.
Using the == operator, Strings do not match is printed

if('user_data_string1' == 'STRING1')
{
    echo 'Good Luck, User Strings match.';
} else {
    echo 'Sorry, Your Strings do not match.';
}

This means that I can’t use the PHP == operator when comparing data strings from user inputs, even if the first letter is in uppercase it will still return false. getting a results Your Strings match. or Your Strings do not match. Therefoe I need to use some other php method to help compare the data strings.

strcmp Function

Next way to compare data strings is to use the PHP function strcmp with results Your Strings match. or Your Strings do not match., this is a binary safe string comparison php method that will return a boolean valude like 0 if the data strings match.

IMP Note: The strcmp() function is binary-safe and case-sensitive.

Syntax of the strcmp

strcmp(user_data_string1,user_data_string2)

Example of the strcmp function

strcmp function, Strings match is printed


if(strcmp('user_data_string1', 'user_data_string1') == 0)
{
     echo 'Good Luck, Strings match.';
} else {
     echo 'Sorry, Your Strings do not match.';
}

This PHP if statement will return true as well as display echo that the user data strings match. But this PHP method is case sensitive Therefor if one of the data strings has an uppercase letter Your Strings match. or Your Strings do not match. then the PHP function will not return boolean value 0.

PHP strcmp() Function – w3schools Example





"; // the two strings are equal
echo strcmp("Welcome pakainfo!","Welcome")."
"; // user_data_string1 is greater than user_data_string2 echo strcmp("Welcome pakainfo!","Welcome pakainfo! Welcome!")."
"; // user_data_string1 is less than user_data_string2 ?>

results:

0
10
-9

strcmp Examples

strcasecmp Function

The my above examples will not suported you to compare different case data strings, the bellow php inbuilt functions with source code will suported you to compare case great way to insensitive data strings.

// together strings will match in this case
if(strcasecmp('user_data_string1', 'user_data_string1') == 0)
{
     echo 'Good Luck, Strings match.';
} else {
     echo 'Sorry, Your Strings do not match.';
}

// together strings will match even with different in this case
if(strcasecmp('user_data_string1', 'String1') == 0)
{
     echo 'Good Luck, Strings match.';
} else {
     echo 'Sorry, Your Strings do not match.';
}

// together strings will match even with different in this case
if(strcasecmp('user_data_string1', 'STRING1') == 0)
{
     echo 'Good Luck, Strings match.';
} else {
     echo 'Sorry, Your Strings do not match.';
}

All of above these PHP if statements will return that the strings match as well as output display like Your Strings match. or Your Strings do not match., which means that we can use this php methods when comparing data strings that are all the input data string by the user.

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about php compare strings.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment