当前位置:网站首页>Why in PHP_ Array (0, ['a','b','c']) returns true
Why in PHP_ Array (0, ['a','b','c']) returns true
2022-06-09 04:34:00 【User 9076598】
stay PHP in , Data meeting Automatic conversion type Then compare .
This may lead to some inexplicable phenomena :
in_array(0, ['a', 'b', 'c']) // return bool(true), It is equivalent to that there are 0 array_search(0, ['a', 'b', 'c']) // return int(0), That is, the subscript of the first value 0 == 'abc' // return bool(true), It is equivalent to equality
Both of these expressions return true.
Intuitively ,0 Not in the array ['a', 'b', 'c'] in , It won't equal abc This string .
How could that return true Well ?
1 Type conversion
And the reason is that , Before comparison ,PHP Type conversion is done .
PHP The explanation on the official website :http://php.net/manual/en/language.types.string.php#language.types.string.conversion
string Data of type will be converted to int type , Then compare .
And if the string The first character of type data is not a number , It will be converted into 0. for example ,
echo intval("Bye"); // Output 0in_array() and array_search() The default is loose comparison , amount to ==, So you get true.
2 Strict comparison
How to get false Well ?
Compare strictly , as follows ,
in_array(0, ['a', 'b', 'c'], true) // return false array_search(0, ['a', 'b', 'c'], true) // return false 0 === 'abc' // return false
Force type comparison , So we can get accurate results .
3 false and null
that , If you use false and null What happens when compared to a string array ?
They will not be converted into int Type , So the result is :
in_array(null, ['a', 'b', 'c']) // return false in_array(false, ['a', 'b', 'c']) // return false
4 Array has true
Another strange phenomenon :
in_array('a', [true, 'b', 'c'])
// return bool(true), It is equivalent to that there are characters in the array 'a'
array_search('a', [true, 'b', 'c'])
// return int(0), Equivalent to finding the character 'a'Why is that ?
It's easy to understand , Relatively loose , whatever string All equal to true.
To be unequal , The old , Compare strictly .
边栏推荐
- Functions and differences of call apply bind
- P5354 [ynoi2017] yoyo OJ (tree section and bit operation)
- P5321 [BJOI2019]送别(LCT)
- How to calculate the rarity of NFT?
- View local public IP
- Golang--- redis operation
- StepN分析
- Test website construction + penetration + audit Part III penetration test of swagger interface
- 2022年安全员-B证上岗证题目及在线模拟考试
- (8) Style binding
猜你喜欢

golang ---image--热力图与照片的重叠

微信小程序:(异常)Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ 解决方案和分析流程(这里一定有你要的答案)

Wuqi_ New progress in vision language navigation: pre training and sim2real

Number precision-- use / instance

Rigidbody2d rotate around a fixed point rotate

记录一次将dmp文件导入oracle数据库(本地导线上),所遇到的问题及解决方法

2022年安全员-A证考试试题及在线模拟考试
![[004] [ESP32开发笔记] 音频开发框架ADF环境搭建——基于ESP-IDF](/img/55/9eb286bc56ec991837fc014b42fc20.png)
[004] [ESP32开发笔记] 音频开发框架ADF环境搭建——基于ESP-IDF
![[006] [esp32 Development notes] burn firmware steps Using Flash Download Tool](/img/a0/5d5e6c076d267c0ffe4c1e8f10e408.png)
[006] [esp32 Development notes] burn firmware steps Using Flash Download Tool

Audio power amplifier circuit (used voice scheme circuit record)
随机推荐
Golang --- comparison operation of various types of variables
My creation anniversary
微信小程序开发的页面组件
Where will the money go in the SaaS industry in 2022?
MySQL: common statistics, grouping statistics and time format conversion
2022 safety officer-b certificate work certificate title and online simulation examination
"Diwen Cup" electronic design competition of Hunan University of Arts and Sciences was successfully concluded
Wechat applet: (exception) expected begin_ Object but was string at line 1 column 1 path $solution and analysis process
2022年R2移动式压力容器充装考试模拟100题及模拟考试
Golang ---image-- overlap of thermal maps and photos
Stepn analysis
Win10安装appium环境
Iscc-2022-reverse-mobile- part WP
音频功率放大电路(使用过的语音方案电路记录)
number-precision--使用/实例
MySQL中的多表查询
[W806捣鼓手记]FPU性能简单测试——2022.05.23
API 網關 Apache APISIX 在 AWS Graviton3 上的安裝與性能測試
2022年安全员-A证考试试题及在线模拟考试
2022 R2 mobile pressure vessel filling test simulation 100 questions and simulation test