博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php比较函数
阅读量:5820 次
发布时间:2019-06-18

本文共 6510 字,大约阅读时间需要 21 分钟。

hot3.png

最近浏览PHP语言的源码-比较函数

其实我说的比较函数不是字符串比较,而且当我用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 的写协议,

转载于:https://my.oschina.net/u/242853/blog/490752

你可能感兴趣的文章
Windows 下最佳的 C++ 开发的 IDE 是什么?
查看>>
软件工程师成长为架构师必备的十项技能
查看>>
python 异常
查看>>
百度账号注销
查看>>
mysql-This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME 错误解决
查看>>
BIEE Demo(RPD创建 + 分析 +仪表盘 )
查看>>
Cocos2dx 3.0开发环境的搭建--Eclipse建立在Android工程
查看>>
基本概念复习
查看>>
重构第10天:提取方法(Extract Method)
查看>>
Android Fragment使用(四) Toolbar使用及Fragment中的Toolbar处理
查看>>
解决pycharm在ubuntu下搜狗输入法一直固定在左下角的问题
查看>>
多线程day01
查看>>
react-native 模仿原生 实现下拉刷新/上拉加载更多(RefreshListView)
查看>>
MySQL出现Access denied for user ‘root’@’localhost’ (using password:YES)
查看>>
通过Roslyn构建自己的C#脚本(更新版)(转)
查看>>
红黑树
查看>>
UIImagePickerController拍照与摄像
查看>>
python调用windows api
查看>>
第四章 mybatis批量insert
查看>>
Java并发框架——什么是AQS框架
查看>>