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,