其实我说的比较函数不是字符串比较,而且当我用php 的数组排序时候需要比较函数
1,为什么用比较函数
首先用比较函数的好处就是代码的抽象,其实就是只有满足比较函数的规则你就可以,按照你自己方式排序,不是降序和升序这些简单的规则!满足规则{-1,0,1}-1:代表是小于,0代表等于,1代表大小
2.用比较函数
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.总结
其实一般人认为比较函数很简单:就返回-1,0,1比较可以吗,为什么写的那么长。首选,php是弱类型语言,我们不能确定我们要比较两个数 是什么类型,比如数字,数字又分整型,浮点型,长整型,字符串,空等于是就注定长php的2个php的类型的个数= 2^(php类型的个数)
这就是一门语言,受欢迎的语言,考虑的方面比较多,全面性,
一门语言存在时间越长越长,它的补丁就越多,这个是在实践中检验出来的!你问它为什么,有可能它也不知道,反正就这样这个问题可以避免,有可能为了环境,你肯定见过#if have_strcoll #endif 等等,也有可能避免它用函数的bug,也有可能每次对代码的抽象的不同,反正就源码就是一个难懂,难懂后面你可以看到一门语言的背后的故事,其实底层和我们做应用层原理一样,就是通过已有创造出我们想要东西,比如应用层:我们可以用php语言写一个web页面,底层只不过用c语言写一个tcp/ip 的写协议,