PHP in_array function usage problems and solutions, phpin_array

Source: Internet
Author: User

PHP in_array function usage problems and solutions, phpin_array

First, we will introduce the background:

Invoice method:

0 = donation (do not ask me why, historical reasons)

1 = forward

2 = request

3 = electronic invoice

Check the data submitted by the user:

Php; auto-links: false; "> if (! In_array ($ _ POST ['invoice _ action'], array (,) {throw new Exception ('select the correct invoice method ');}

There is a problem at this time, if it does not exist at all$_POST[‘invoice_action']Why didn't an exception be thrown?

It has been confirmed that this is a pitfall of PHP as a weak language.

Let's take a look at this set of code:

Echo in_array ('', array (0 ))? 1: 0; // result: 1 echo in_array (null, array (0 ))? 1: 0; // result: 1 echo in_array (false, array (0 ))? 1: 0; // result: 1

How can we bypass or fill in such a big pitfall?

Method 1: in_array supports the third parameter, which forces data type detection.

Echo in_array ('', array (0), true )? 1: 0; // result: 0 echo in_array (null, array (0), true )? 1: 0; // result: 0 echo in_array (false, array (0), true )? 1: 0; // result: 0

Method 2: change the value 0 in the array to a string.

Echo in_array ('', array ('0'), true )? 1: 0; // result: 0 echo in_array (null, array ('0'), true )? 1: 0; // result: 0 echo in_array (false, array ('0'), true )? 1: 0; // result: 0

Summary

The above are the problems and solutions encountered when using the in_array function in PHP. I hope this article will be helpful to anyone who has encountered this problem. If you have any questions, please leave a message.

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.