Thoughts on the automatic transformation in PHP comparison operations caused by the in_array () function

Source: Internet
Author: User
Tags vars
1 problem Origin

The problem stems from a detailed tutorial on PHP's teaching video: PhP's super easy-to-use File Upload processing class. In the video, the constructor uses arrays to pass parameters, and then assigns values to members based on whether these parameter names are class member names. The code snippet is as follows:

1 <? PHP
2 class fileupload
3 {
4 // specify the upload path, the allowed type, the file size limit, and whether to use a random file name
5 private $ file_path;
6 private $ allowed_types = array ('gif', 'png ', 'jpg', 'jpeg ');
6 private $ max_size = 100000;
8 private $ name_random = true;
9
10 function _ construct ($ options ){
11 foreach ($ options as $ key => $ value ){
12 $ key = strtolower ($ key );
13 $ vars = get_class_vars (get_class ($ this ));
14 if (! In_array ($ key, $ vars )){
15 continue;
16}
17 $ this-> $ key = $ value;
18}
19}
20
21 function _ destruct (){
22}
23}

The first note is that in_array () should not be used here, because in_array is used to judge the value of the array element rather than the index key value of the element, this point has been changed to array_key_exists on page 371 of PHP version 2.

However, the above functions are easy to use here, because in fact, no matter what parameters are passed in, the in_array () in row 14th will return true, what we need to analyze is why true is always returned.

2. The cause analysis problem lies in row 8th, where the value of a member is true. During PHP comparison, when the two values of different types are compared, the system automatically performs the transformation. The rules are as follows:
Null or string String SetNULLConvert to "" For comparison of numbers or words
Bool or null Any other type Convert to bool,FALSE<TRUE
Object Object Built-in classes can define their own comparisons. Different classes cannot be compared. The same classes and arrays are used to compare attributes (in PHP 4). PHP 5 has its own instructions.
String, resource, or number String, resource, or number Converts a string and a resource to a number and compares it by common mathematics.
Array Array The array with fewer members is small. If the key in Operation Number 1 does not exist in Operation Number 2, the array cannot be compared. Otherwise, compare the values one by one (see the following example)
Array Any other type Array is always larger
Object Any other type The object is always larger.


Obviously, the second rule is hit here, because the caller inputs a string. If the string is compared with this true, as long as it is not empty, before the comparison, it will be automatically converted to true of the bool value, of course, true = true.
The simplified model is as follows:In_array (any non-empty string, array (element 1, element 2..., true ));This function always returns true.3. Conclusion (1) when using the PHP function, you must understand its correct meaning. For example, the in_array here should be changed to array_key_exists. (2) PHP soft type automatic type conversion is very frequent, need to pay attention. In fact, in_array () also provides the third optional parameter to further determine whether to use = rather than =.

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.