How to avoid calling In_array () function error in PHP programming

Source: Internet
Author: User
Tags php language php programming

PHP development process, often encountered to determine whether a one-dimensional array contains an element, this will be used to In_array (), because PHP is a weak type of language, so careless, will also step on the "pit." (Note: The PHP version used in this test is 5.6.) So how do you pay attention? Here are some practical experiences to share with you.

There's a pit ahead.

Directly on the code:

$arr = [' PHP on the Road ', 1];

Echo In_array (True, $arr)? 1:0;

Echo in_array (0, $arr)? 1:0;

Results:

11

Second, why there is a pit

In fact, from the bottom source, In_array () is also used a cycle by comparison method. And the previous installments we said, ' PHP on the road ' ==true, ' PHP on the road ' ==0 these two are set up.

Viewed online, someone uses for to judge.

PHP is a weak type language, the definition and use of variables is not strict, most of the "pit" is derived from this point. However, this "pit" PHP language means not to back this pit, In_array () function represents grievance.

This is the reason why the user did not read the manual for In_array (), read the manual:

Function:

BOOL In_array (mixed $needle, array $haystack [, bool $strict = FALSE])

Parameters:

Needle: The value to find.

Haystack: Array to find

Strict: Strict match, TRUE or FALSE

return value

Returns TRUE if needle is found, otherwise FALSE.

The point is, the key is in the third parameter: strict, if true, the type is compared before the comparison value. The default is False,false,false,false ... The reason is here!

So, do not omit the third argument when using the In_array () function.

Third, anti-crater expansion

1. In the In_array () function, the needle argument is case-sensitive.

2, sometimes when the array search for a given value, if you also want to know the index of the given value, it will use the Array_search () function. This function also has a third parameter and is also a strict match.

In addition, since the Array_search () function returns the index of the given value, the return value may be 0 or false, so use "= = =" To determine whether a given value exists for subsequent judgments.

Such as:

$arr = [' n ', ' PHP on the Road '];

Echo Array_search (' A ', $arr, true) ===false? ' Not found ': ' Found ';

Iv. Anti-crater interaction

Look at the code:

$arr = [' PHP on the road ', 12, ', [' I am a string ']];

Echo In_array ([0], $arr)? 1:0;

Now everyone knows why the call In_array () error, then in the future code must pay attention to, if there is no understanding of the place or there are other aspects of the development of the question, can be a message consulting to get help.

This article by the Professional Zhengzhou app development company Brigitte Xuan Science and Technology finishing release, original is not easy, reprint please indicate source and author.

How to avoid calling In_array () function error in PHP programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.