Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

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

Shulou Source: shulou.com Published: 2022-06-02 03:20:49 09月13日 Update

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.

Tags: Arrays characters types strings results content skills data articles knowledge language questions explanations loose concise intuitive concise subscript examples accuracy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Apple Microsoft Huawei MySQL