Php comparison function recently browsed the source code of the PHP language-comparison function
In fact, the comparison function I mentioned is not a string comparison function, and when I use PHP to sort arrays, I need to compare the function.
1. why use comparison functions?
First, the advantage of comparing functions is the code Abstraction. In fact, only the rules that meet the comparison functions can be sorted by yourself, rather than the simple rules in descending order and ascending order! Rule {-1, 0, 1}-1: indicates that the value is smaller than, 0 indicates that the value is equal to, and 1 indicates the size.
2. use comparison functions
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_LONG, 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) { ZVAL_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, most people think that the comparison function is very simple: is it possible to return-1, 0, 1, why is it so long. First, php is a weak language. we cannot determine the type of the two numbers to be compared, such as numbers, numbers, and integer, floating point, long, and string, null equals to the number of two PHP types = 2 ^ (number of PHP types)
This is a popular language with many considerations and comprehensive features,
The longer a language has, the more patches it has. this is proven in practice! If you ask why, maybe it doesn't know. in this case, you can avoid this problem. it may be for the sake of the environment. you must have seen # if HAVE_STRCOLL # endif and so on, it is also possible to avoid the bug of using functions. it is also possible that every time the code is abstracted differently, the source code is hard to understand and you can see the story behind a language, in fact, the underlying layer works the same way as the application layer. it creates what we want through the existing layer. for example, the application layer: we can use php to write a web page, the underlying layer only writes a TCP/IP write protocol in C language,