PHP容易被忽略而出错陷阱 数字与字符串比较_php技巧
0 与任意非数字(或者说,不可转化为数字的字符)前导的字符串比较(操作符为==), 均返回 true.
原因是, 数字与字符串比较时, 先尝试将字符串转换为数字, 再比较, 一个不能转换为数字的字符串, 转换结果为0, 故, 与0比较总返回 true.
更加详细的比较规则, 多种类型的比较规则, 在 PHP手册/语言参考/运算符/比较运算符 可以找到.
在PHP里当两个数字型字符串(只含数字的字符串)进行比较的时候是直接转换成数值进行比较的
如下示例:(注意$a和$b两个变量的最后一位不相等)
//示例1
$a = '511203199106034578';
$b = '511203199106034579';
if ($a==$b) {
echo 'equal';
} else {
echo 'notEqual';
}
?>
运行上面的程序却发现结果为equal(非我们认为的结果)
我们把$a与$b分别加一个字母a进去
//示例2
$a = 'a511203199106034578';
$b = 'a511203199106034579';
if ($a==$b) {
echo 'equal';
} else {
echo 'notEqual';
}
?>
这次输出的是notEqual(正确的结果)
示例1为equal是因为PHP把两个数字型字符串转换成数字型,而这两个数字刚好相等如下示例
$a = 511203199106034578;
$b = 511203199106034579;
echo $a; // 输出 5.1120319910603E+17 即511203199106030000
echo $b; // 输出 5.1120319910603E+17 即511203199106030000
?>
所以我们在示例1中得到的结果是equal
避免出现这种非预期结果的情况是使用类型比较符===如下示例(如果 $a 等于 $b,并且它们的类型也相同)
//示例4
$a = '511203199106034578';
$b = '511203199106034579';
if ($a===$b) {
echo 'equal';
} else {
echo 'notEqual';
}
?>
这样我们就可以得到预期中的notEqual了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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 "Connect and Share" or "Wireless & Networks" option. Step 3: In the Connection & Sharing or Wireless & Networks menu, find and click "NFC & Payments". Step 4: Find and click "NFC Switch". Normally, the default is off. Step 5: On the NFC switch page, click the switch button to switch it to on.

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.

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,

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

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.

Differences and comparative analysis between C language and PHP C language and PHP are both common programming languages, but they have obvious differences in many aspects. This article will conduct a comparative analysis of C language and PHP and illustrate the differences between them through specific code examples. 1. Syntax and usage: C language: C language is a process-oriented programming language, mainly used for system-level programming and embedded development. The syntax of C language is relatively simple and low-level, can directly operate memory, and is efficient and flexible. C language emphasizes the programmer's completeness of the program

What should I do if the computer freezes after AMDCAD is upgraded to win11? If the startup speed or page loading is slow after AMDCAD software is installed, it may be because the original system settings are not optimal. Let’s take a look at the solutions for win11~ 1. Hardware The configuration is not enough to support the animation and special effects of the win11 system. At this time, we can first enter the system "Settings" through the start menu interface. 2. Search for "Performance" in the search box on the upper left and enter "Adjust the appearance and performance of Windows". 3. Check "Adjust for best performance" at the bottom of the visual effects interface. 4. After the settings are completed, click &

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.
