php比较运算符的bug
疑问
首选,给出一个php的示例代码,如果你完全明白为什么会出现如下结果,就不需要再在这篇博客浪费时间了,这里是让php初学者注意一下可能会犯的错误
[php]
var_dump("abcdefg" == "0"); // bool(false)
var_dump("abdsafd" == 0); // bool(true)
var_dump("abcdefg" == "0"); // bool(false)
var_dump("abdsafd" == 0); // bool(true)
如果你不是太清楚上面的结果,那就跟着博客继续学习下去吧!
比较运算符
比较运算符,如同它们名称所暗示的,允许对两个值进行比较。
比较运算符 例子 名称 结果
$a == $b 等于 TRUE,如果$a等于$b
$a === $b 全等 TRUE,如果$a等于$b,并且它们的类型也相同
$a != $b 不等 TRUE,如果$a不等于$b
$a $b 不等 TRUE,如果$a不等于$b
$a !== $b 非全等 TRUE,如果$a不等于$b,或者它们的类型不同
$a
$a > $b 大于 TRUE,如果$a严格大于$b
$a
$a >= $b 大于等于 TRUE,如果$a大于或者等于$b
注意:
如果比较一个整数和字符串,则字符串会被转换为整数
如果比较两个数字字符串,则作为整数比较
讲到这里,就可以说一下上面那个php代码了,var_dump("abcdefg" == "0")为false,是因为这是两个字符串比较,对应于c的strcmp函数,因此应该为false。然而,var_dump("abdsafd" == 0)为true,则需要学习一下字符串转换为整数的规则了
字符串转换为整数
当一个字符串被用在了一个数字的环境中,结果和类型如下:
如果字符串没有包含‘.’、'e'或'E',并且数字值符合整数类型的限定(PHP_INT_MAX定义的),这个字符串可被认定是一个integer,在其它情况下被认定为一个float
字符串的开始部分给定了它的值,如果字符串以合法的数字开始,这个数字就可直接使用。否则,值就是0.合法数值由符号,后面跟着一个或多个数字(可能有个小数点),再跟着可选的指数符号如'e'或'E',后面跟着一个或多个数字。
示例代码:
[php]
$foo = 1 + "10.5"; // $foo is float (11.5)
$foo = 1 + "bob-1.3e3"; // $foo is integer (1)
$foo = 1 + "bob3"; // $foo is integer (1)
$foo = 1 + "10 Small Pigs"; // $foo is integer (11)
$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
$foo = "10.0 pigs " + 1; // $foo is float (11)
$foo = "10.0 pigs " + 1.0; // $foo is float (11)
?>
$foo = 1 + "10.5"; // $foo is float (11.5)
$foo = 1 + "bob-1.3e3"; // $foo is integer (1)
$foo = 1 + "bob3"; // $foo is integer (1)
$foo = 1 + "10 Small Pigs"; // $foo is integer (11)
$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
$foo = "10.0 pigs " + 1; // $foo is float (11)
$foo = "10.0 pigs " + 1.0; // $foo is float (11)
?>
这里应该可以彻底解释var_dump("abcdefg" == 0)为true了,因为比较运算符首先将“abcdefg”强制转换为整数0,因为0==0为true了

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

Local fine-tuning of DeepSeek class models faces the challenge of insufficient computing resources and expertise. To address these challenges, the following strategies can be adopted: Model quantization: convert model parameters into low-precision integers, reducing memory footprint. Use smaller models: Select a pretrained model with smaller parameters for easier local fine-tuning. Data selection and preprocessing: Select high-quality data and perform appropriate preprocessing to avoid poor data quality affecting model effectiveness. Batch training: For large data sets, load data in batches for training to avoid memory overflow. Acceleration with GPU: Use independent graphics cards to accelerate the training process and shorten the training time.

There are a variety of reasons why DeepSeek cannot log in, including server failure, network connection issues, account disables, login credentials errors, or system updates. When you cannot enter the DeepSeek login portal, users can access the official website through https://www.deepseek.com/. If you encounter login problems, users should check the server status and troubleshoot network connection or login credential errors. If the problem persists, users should contact the DeepSeek support team for further assistance.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Why can’t the Bybit exchange link be directly downloaded and installed? Bybit is a cryptocurrency exchange that provides trading services to users. The exchange's mobile apps cannot be downloaded directly through AppStore or GooglePlay for the following reasons: 1. App Store policy restricts Apple and Google from having strict requirements on the types of applications allowed in the app store. Cryptocurrency exchange applications often do not meet these requirements because they involve financial services and require specific regulations and security standards. 2. Laws and regulations Compliance In many countries, activities related to cryptocurrency transactions are regulated or restricted. To comply with these regulations, Bybit Application can only be used through official websites or other authorized channels

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This article introduces free digital asset quotation software apps and websites that can provide investors with key information such as real-time prices, price charts, transaction volume, fluctuations, market depth and news information to help investors make informed decisions. Compared with paid software, free software has the advantages of no cost, rich features, and easy operation. The article also guides users how to choose the right market software, and reminds users to pay attention to data sources, information accuracy and avoid excessive dependence, which ultimately helps investors better grasp the trends of the digital asset market. Want to know how to use free market software efficiently? Read the full text quickly!

Original title: Bittensor=AIBitcoin? Original author: S4mmyEth, Decentralized AI Research Original translation: zhouzhou, BlockBeats Editor's note: This article discusses Bittensor, a decentralized AI platform, hoping to break the monopoly of centralized AI companies through blockchain technology and promote an open and collaborative AI ecosystem. Bittensor adopts a subnet model that allows the emergence of different AI solutions and inspires innovation through TAO tokens. Although the AI market is mature, Bittensor faces competitive risks and may be subject to other open source

Gateio Exchange app download channels for old versions, covering official, third-party application markets, forum communities and other channels. It also provides download precautions to help you easily obtain old versions and solve the problems of discomfort in using new versions or device compatibility.
