In the previous article, we learned about the method of replacing specified characters in a string. If necessary, please read "Detailed introduction to the method of replacing specified characters in PHP (case sensitive)". This time we will introduce to you the method of comparing two strings. You can refer to it if you need it.
Sometimes we may need to compare two strings. At this time, we need to use the function to compare strings, so today we will introduce the method of comparing two strings.
First let’s look at the first one, using the strcasecmp() function.
Let’s look at a small example.
<?php echo strcasecmp("yellow rattan wine","Yellow Rattan Wine"); ?>
Let’s take a look at the results of this example
0
My friend, do you have a lot of question marks? What does this 0 mean? Don’t worry, let’s learn about this function.
strcasecmp() function compares two strings.
Let’s take a look at the syntax of this function
strcasecmp(要比较的第一个字符串,要比较的第二个字符串)
The most important thing is the return value of this function, let’s take a look.
The function returns:
0 - if the two strings are equal
<0 - if string1 is less than string2
>0 - if string1 is greater than string2
This is the end of the introduction of the first function. Next we Introducing the second method, the strcmp() function.
Let’s take a look at a little chestnut.
<?php echo strcmp("yellow rattan wine","Yellow Rattan Wine"); ?>
Look at the result of this chestnut
32? What does this mean? Is this the same function as the one above? Let's learn it now.
strcmp() function compares two strings.
Let’s take a look at the syntax of this function
strcmp(要比较的第一个字符串,要比较的第二个字符串)
This function is the same as the strcasecmp function above. The returned value is greater than 0, which means that the first character is greater than the second character.
That’s all. If you want to know anything else, you can click here. → →php video tutorial
The above is the detailed content of How to know the similarities and differences of strings in php function. For more information, please follow other related articles on the PHP Chinese website!