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.