PHP comparison function

Source: Internet
Author: User
Recently viewed source code-comparison function for PHP language

Actually, the comparison function I'm talking about is not a string comparison, and I need to compare functions when I sort the array in PHP.

1, why use a comparison function

The advantage of using the comparison function first is the abstraction of the code, in fact it is only the rules that satisfy the comparison function you can, sort by your own way, not descending and ascending these simple rules! Satisfies the rule { -1,0,1}-1: represents is less than, 0 represents equals, 1 represents size

2. Using the comparison function

Zend_api int compare_function (zval *result, Zval *op1, Zval *op2)/* {{{*/{int ret;        int converted = 0;        Zval op1_copy, op2_copy;        Zval *op_free, Tmp_free; while (1) {switch (Type_pair (z_type_p (OP1), z_type_p (OP2))) {Case Type_pair (Is_lon G, Is_long): Zval_long (Result, z_lval_p (OP1) >z_lval_p (OP2)? 1: (Z_lval_p (OP1)
 
  
Get) {Zval RV;                                                Op_free = z_obj_ht_p (OP1)->get (OP1, &RV);                                                ret = compare_function (result, Op_free, OP2);                                                Zend_free_obj_get_result (Op_free);                                        return ret;                                                } else if (Z_type_p (OP2)! = Is_object && z_obj_ht_p (OP1)->cast_object) {                                                Zval_undef (&tmp_free); if (z_obj_ht_p (OP1)->cast_object (OP1, &tmp_free, ((z_type_p (OP2) = = Is_false | | Z_type_p (OP2) = = is_true)?                                                        _is_bool:z_type_p (OP2)) = = FAILURE) {Zval_long (result, 1);                                  Zend_free_obj_get_result (&tmp_free);                      return SUCCESS;                                                } ret = Compare_function (result, &tmp_free, OP2);                                                Zend_free_obj_get_result (&tmp_free);                                        return ret;                                        }} if (Z_type_p (op2) = = Is_object) {                                                if (z_obj_ht_p (OP2)->get) {Zval rv;                                                Op_free = z_obj_ht_p (OP2)->get (OP2, &RV);                                                ret = compare_function (result, OP1, op_free);                                                Zend_free_obj_get_result (Op_free);                                        return ret; } else if (Z_type_p (OP1)! = Is_obJect && z_obj_ht_p (OP2)->cast_object) {Zval_undef (&tmp_free                                                ); if (z_obj_ht_p (OP2)->cast_object (OP2, &tmp_free, ((z_type_p (OP1) = = Is_false | | Z_type_p (OP1) = = is_true)?                                                        _is_bool:z_type_p (OP1)) = = FAILURE) {Zval_long (result,-1);                                                        Zend_free_obj_get_result (&tmp_free);                                                return SUCCESS;                                                } ret = Compare_function (result, OP1, &tmp_free);                                                Zend_free_obj_get_result (&tmp_free);                                        return ret; } else if (z_type_p (OP1) = = Is_object) {Zval_LONG (result, 1);                                        return SUCCESS;                                        }} if (!converted) { if (z_type_p (OP1) = = Is_null | |                                                 Z_type_p (OP1) = = Is_false) {Zval_long (result, zval_is_true (OP2)? -1:0);                                        return SUCCESS; } else if (z_type_p (op2) = = Is_null | |                                                Z_type_p (OP2) = = Is_false) {Zval_long (result, zval_is_true (OP1)? 1:0);                                        return SUCCESS; } else if (z_type_p (OP1) = = is_true) {Zval_long (result, zval_is_true (OP2) ?                                                0:1);                                        return SUCCESS; } ElSe if (z_type_p (op2) = = is_true) {Zval_long (result, zval_is_true (OP1)? 0                                                :-1);                                        return SUCCESS;                                                } else {Zendi_convert_scalar_to_number (OP1, op1_copy, result);                                                Zendi_convert_scalar_to_number (OP2, op2_copy, result);                                        converted = 1; }} else if (Z_type_p (OP1) ==is_array) {Zva                                        L_long (result, 1);                                return SUCCESS;                                        } else if (Z_type_p (OP2) ==is_array) {Zval_long (result,-1);                                return SUCCESS;    } else if (Z_type_p (OP1) ==is_object) {                                    Zval_long (result, 1);                                return SUCCESS;                                        } else if (Z_type_p (OP2) ==is_object) {Zval_long (result,-1);                                return SUCCESS;                                        } else {Zval_long (result, 0);                                return FAILURE; }                }        }}
 

3. Summary

In fact, the general people think the comparison function is very simple: return -1,0,1 comparison is OK, why write so long. Preferred, PHP is a weakly typed language, we are not sure we want to compare two numbers is what type, such as numbers, numbers and integral type, floating point type, long Integer, string, empty and so on is doomed to long PHP 2 php type number = 2^ (number of PHP type)

This is a language, a popular language, a more comprehensive consideration,

The longer the time a language exists, the more patches it has, and this is tested in practice! You ask why, it is possible that it does not know, anyway this problem can be avoided, it is possible for the environment, you must have seen the # if Have_strcoll #endif and so on, it is possible to avoid it with the function of the bug, it is possible each time the code of the abstract different, anyway, the source is a difficult to understand, Difficult to understand behind you can see a language behind the story, in fact, the bottom and we do the application layer principle, is through the already created we want things, such as the application layer: we can write a Web page in PHP language, the bottom is only in C language to write a TCP/IP write protocol,

  • 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.