Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why did PHP in_array (0, [& # 039 * & # 039 *, & # 039 *, & # 039 *, *

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you why PHP in_array (0, ['await,' baked,'c']) returns to true. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

0. Question background

In_array (0, ['a', 'baked,' c']) / / returns bool (true), which is equivalent to 0 in the array

Array_search (0, ['a', 'baked,' c']) / / returns int (0), which is the subscript of the first value

0 = 'abc' / / returns bool (true), which is equivalent

How do you explain the above return results?

1. Type conversion

The reason: PHP does a type conversion before comparing the data. Citing PHP's official website, the explanation for "String conversion to numbers" is as follows:

When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

In all other cases it will be evaluated as a float.

The value is given by the initial portion of the string. If the string starts with valid numeric data, this

Will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an'e'or'E 'followed by one or more digits.

In the example at the beginning of the article, if the first character of string type data is not a number, it will be converted to 0, for example:

Echo intval ('abc'); / / output 0

Both inarray () and arraysearch () are loose comparisons by default, which is equivalent to = =, that is, true.

2. Strict comparison

So how do we get the results we expect? Use a strict comparison, as follows:

Array_search (0, ['averse,' baked,'c'], true) / / returns bool (false)

0 = = 'abc' / / returns bool (false) 3, false and null

So what happens if you compare an array of strings with false and null?

In_array (null, ['averse,' baked,'c']) / / returns bool (false)

In_array (false, ['averse,' baked,'c']) / / returns bool (false)

Null is compared with false, and the string array is not converted to int.

4. There is true in the array

Another strange-looking phenomenon

Array_search ('asides, [true,' breadth,'c']) / / returns int (0), which is equivalent to finding the string'a'

PHP language itself is a weakly typed language, in order to facilitate application processing, it will do some type conversion operations.

At the same time, in order to ensure the accuracy and accuracy of the conversion, PHP officials suggest that unknown scores should not be forced to be converted to integer, which can sometimes lead to unpredictable results.

The above is why PHP in_array (0, ['await,' baked,'c']) returns to true. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report