First, introduce the requirements background:
Invoice method:
0= donation (Don't ask me why, historical reasons)
1= sent to China
2= Request
3= Electronic Invoice
Now you want to test the data submitted by the User:
if (!in_array ($_post[' invoice_action '), Array (0,1,2,3))) {
throw new Exception (' Please select the correct invoice method ');
}
This time there is a problem, if there is no $_post[' invoice_action ' value, why not throw an exception?
This is confirmed as a pit of PHP as a weakly typed language!!! Yes, it's a pit!!!.
Take a look at this set of code:
Echo In_array (', array (0))? 1:0; Results: 1
echo In_array (null, array (0))? 1:0; Results: 1
Echo In_array (False, array (0))? 1:0; Results: 1
How are we going to get around or fill out this big hole?
Method One: In_array supports the third parameter, forcing data type detection
Echo In_array (", array (0), true)?" 1:0; Results: 0
echo In_array (null, array (0), true)? 1:0; Results: 0
Echo In_array (False, array (0), true)? 1:0; Results: 0
Method Two: Still is the data type direction, changes the array 0 to the string
Echo In_array (', Array (' 0 '), true)? 1:0; //Result: 0
Echo in_array (null, Array (' 0 '), t Rue)? 1:0; //Result: 0
Echo In_array (False, Array (' 0 '), true)? 1:0; //Result: 0