Home Backend Development PHP Tutorial How to compare the size of strings in PHP?

How to compare the size of strings in PHP?

May 10, 2017 pm 05:34 PM

In WEB project development, it is often necessary to determine the size between two or more strings. However, we cannot determine the size of two strings by looking at two characters. So how to determine the size of two or more strings? As for the sizes of multiple strings, this article will take you through, How to compare the sizes of strings?

First we learn two functions strcmp() and strcasecmp() functions

These two functions are used to compare two The size of the string, their syntax is as follows:

strcmp(str1,str2)
Copy after login
strcasecmp(str1,str2)
Copy after login

These two functions are based on (ASCII code value) comparison of the size of the two strings, if str1>str2 returns 1; if str1< ;Str2 returns -1; if str1=str2, returns 0. The difference between them is that the function strcasecmp() is not case-sensitive during the comparison process, while the function strcmp() is case-sensitive during the comparison process.

Application Example

The strcasecmp() function and strcmp() function are used below to compare string sizes.

The code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>字符串大小写转换</title>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
    <p>
        字符串1:<input type="text" name="str1">
    </p>

    <p>
        字符串2:<input type="text" name="str2">
    </p>


    <input type="submit" name="sub" value="比较" />
</form>
</body>
</html>
<?PHP
header("content-type:text/html;charset=utf-8");
$str1=$_POST[&#39;str1&#39;];
$str2=$_POST[&#39;str2&#39;];

if($_POST[&#39;sub&#39;]){
 $result=strcmp($str1,$str2);
 if($result==1){
 echo "<div>字符串&#39;".$str1."&#39; > 字符串&#39;".$str2."&#39;</div>";
    }
 if($result==0){
 echo "<div>字符串&#39;".$str1."&#39; = 字符串&#39;".$str2."&#39;</div>";
    }
 if($result==-1){
 echo "<div>字符串&#39;".$str1."&#39; < 字符串&#39;".$str2."&#39;</div>";
    }
}
?>
Copy after login

The result of running the code is as follows:

How to compare the size of strings in PHP?

The above example uses the strcmp() function to compare two characters String, if you are interested, you can replace the strcmp() function with the strcasecmp() function to try and see what the difference is.

How to compare the size of strings in PHP?Comparing the size of strings mainly focuses on comparing types and cases. In actual applications, when encountering the same English word, if its case is different, the operation result will be changed. If an error occurs, you can compare the strings first and then take the next step according to the situation.

【Related Recommendations】

1. How to convert strings to uppercase and lowercase in php?

2. php video tutorial: "php.cn Dugu Jiujian (4) - php video tutorial"

3. php string Topic

The above is the detailed content of How to compare the size of strings in PHP?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to enable nfc function on Xiaomi Mi 14 Pro? How to enable nfc function on Xiaomi Mi 14 Pro? Mar 19, 2024 pm 02:28 PM

Nowadays, the performance and functions of mobile phones are becoming more and more powerful. Almost all mobile phones are equipped with convenient NFC functions to facilitate users for mobile payment and identity authentication. However, some Xiaomi 14Pro users may not know how to enable the NFC function. Next, let me introduce it to you in detail. How to enable nfc function on Xiaomi 14Pro? Step 1: Open the settings menu of your phone. Step 2: Find and click the &quot;Connect and Share&quot; or &quot;Wireless &amp; Networks&quot; option. Step 3: In the Connection &amp; Sharing or Wireless &amp; Networks menu, find and click &quot;NFC &amp; Payments&quot;. Step 4: Find and click &quot;NFC Switch&quot;. Normally, the default is off. Step 5: On the NFC switch page, click the switch button to switch it to on.

How to use TikTok on Huawei Pocket2 remotely? How to use TikTok on Huawei Pocket2 remotely? Mar 18, 2024 pm 03:00 PM

Sliding the screen through the air is a feature of Huawei that is highly praised in the Huawei mate60 series. This feature uses the laser sensor on the phone and the 3D depth camera of the front camera to complete a series of functions that do not require The function of touching the screen is, for example, to use TikTok from a distance. But how should Huawei Pocket 2 use TikTok from a distance? How to take screenshots from the air with Huawei Pocket2? 1. Open the settings of Huawei Pocket2 2. Then select [Accessibility]. 3. Click to open [Smart Perception]. 4. Just turn on the [Air Swipe Screen], [Air Screenshot], and [Air Press] switches. 5. When using it, you need to stand 20~40CM away from the screen, open your palm, and wait until the palm icon appears on the screen.

How to increase disk size in VirtualBox [Guide] How to increase disk size in VirtualBox [Guide] Mar 17, 2024 am 10:10 AM

We often encounter situations where the predefined disk size has no room for more data? If you need more virtual machine hard disk space at a later stage, you must expand the virtual hard disk and partitions. In this post, we will see how to increase disk size in VirtualBox. Increasing the disk size in VirtualBox It is important to note that you may want to back up your virtual hard disk files before performing these operations, as there is always the possibility of something going wrong. It is always a good practice to have backups. However, the process usually works fine, just make sure to shut down your machine before continuing. There are two ways to increase disk size in VirtualBox. Expand VirtualBox disk size using GUI using CL

How to set line spacing in WPS Word to make the document neater How to set line spacing in WPS Word to make the document neater Mar 20, 2024 pm 04:30 PM

WPS is our commonly used office software. When editing long articles, the fonts are often too small to be seen clearly, so the fonts and the entire document are adjusted. For example: adjusting the line spacing of the document will make the entire document very clear. I suggest that all friends learn this operation step. I will share it with you today. The specific operation steps are as follows, come and take a look! Open the WPS text file you want to adjust, find the paragraph setting toolbar in the [Start] menu, and you will see the small line spacing setting icon (shown as a red circle in the picture). 2. Click the small inverted triangle in the lower right corner of the line spacing setting, and the corresponding line spacing value will appear. You can choose 1 to 3 times the line spacing (as shown by the arrow in the figure). 3. Or right-click the paragraph and it will appear.

TrendX Research Institute: Merlin Chain project analysis and ecological inventory TrendX Research Institute: Merlin Chain project analysis and ecological inventory Mar 24, 2024 am 09:01 AM

According to statistics on March 2, the total TVL of Bitcoin’s second-layer network MerlinChain has reached US$3 billion. Among them, Bitcoin ecological assets accounted for 90.83%, including BTC worth US$1.596 billion and BRC-20 assets worth US$404 million. Last month, MerlinChain’s total TVL reached US$1.97 billion within 14 days of launching staking activities, surpassing Blast, which was launched in November last year and is also the most recent and equally eye-catching. On February 26, the total value of NFTs in the MerlinChain ecosystem exceeded US$420 million, becoming the public chain project with the highest NFT market value besides Ethereum. Project Introduction MerlinChain is an OKX support

How to intercept a string in Go language How to intercept a string in Go language Mar 13, 2024 am 08:33 AM

Go language is a powerful and flexible programming language that provides rich string processing functions, including string interception. In the Go language, we can use slices to intercept strings. Next, we will introduce in detail how to intercept strings in Go language, with specific code examples. 1. Use slicing to intercept a string. In the Go language, you can use slicing expressions to intercept a part of a string. The syntax of slice expression is as follows: slice:=str[start:end]where, s

Detailed explanation of the method of converting int type to string in PHP Detailed explanation of the method of converting int type to string in PHP Mar 26, 2024 am 11:45 AM

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

How to repeat a string in python_python repeating string tutorial How to repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

See all articles