How to know the similarities and differences of strings in php function

醉折花枝作酒筹
Release: 2023-03-11 21:52:01
Original
2127 people have browsed it

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");
?>
Copy after login

Let’s take a look at the results of this example

0
Copy after login

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(要比较的第一个字符串,要比较的第二个字符串)
Copy after login

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");
?>
Copy after login

Look at the result of this chestnut

How to know the similarities and differences of strings in php function

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(要比较的第一个字符串,要比较的第二个字符串)
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template