php比较函数

Jun 23, 2016 pm 01:28 PM

最近浏览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)<Z_LVAL_P(op2)?-1:0));                                return SUCCESS;                        case TYPE_PAIR(IS_DOUBLE, IS_LONG):                                Z_DVAL_P(result) = Z_DVAL_P(op1) - (double)Z_LVAL_P(op2);                                ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_DVAL_P(result)));                                return SUCCESS;                        case TYPE_PAIR(IS_LONG, IS_DOUBLE):                                Z_DVAL_P(result) = (double)Z_LVAL_P(op1) - Z_DVAL_P(op2);                                ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_DVAL_P(result)));                                return SUCCESS;                           case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE):                                if (Z_DVAL_P(op1) == Z_DVAL_P(op2)) {                                        ZVAL_LONG(result, 0);                                } else {                                        Z_DVAL_P(result) = Z_DVAL_P(op1) - Z_DVAL_P(op2);                                        ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_DVAL_P(result)));                                }                                return SUCCESS;                        case TYPE_PAIR(IS_ARRAY, IS_ARRAY):                                ZVAL_LONG(result, zend_compare_arrays(op1, op2));                                return SUCCESS;                        case TYPE_PAIR(IS_NULL, IS_NULL):                        case TYPE_PAIR(IS_NULL, IS_FALSE):                        case TYPE_PAIR(IS_FALSE, IS_NULL):                        case TYPE_PAIR(IS_FALSE, IS_FALSE):                        case TYPE_PAIR(IS_TRUE, IS_TRUE):                                ZVAL_LONG(result, 0);                                return SUCCESS;                        case TYPE_PAIR(IS_NULL, IS_TRUE):                                ZVAL_LONG(result, -1);                                return SUCCESS;                        case TYPE_PAIR(IS_TRUE, IS_NULL):                                ZVAL_LONG(result, 1);                                return SUCCESS;                        case TYPE_PAIR(IS_STRING, IS_STRING):                                if (Z_STR_P(op1) == Z_STR_P(op2)) {                                        ZVAL_LONG(result, 0);                                        return SUCCESS;                                }                                ZVAL_LONG(result, zendi_smart_strcmp(op1, op2));                                return SUCCESS;                        case TYPE_PAIR(IS_NULL, IS_STRING):                                ZVAL_LONG(result, Z_STRLEN_P(op2) == 0 ? 0 : -1);                                return SUCCESS;                        case TYPE_PAIR(IS_NULL, IS_STRING):                                ZVAL_LONG(result, Z_STRLEN_P(op2) == 0 ? 0 : -1);                                return SUCCESS;                        case TYPE_PAIR(IS_STRING, IS_NULL):                                ZVAL_LONG(result, Z_STRLEN_P(op1) == 0 ? 0 : 1);                                return SUCCESS;                        case TYPE_PAIR(IS_OBJECT, IS_NULL):                                ZVAL_LONG(result, 1);                                return SUCCESS;                        case TYPE_PAIR(IS_NULL, IS_OBJECT):                                ZVAL_LONG(result, -1);                                return SUCCESS;                        default:                                if (Z_ISREF_P(op1)) {                                        op1 = Z_REFVAL_P(op1);                                        continue;                                } else if (Z_ISREF_P(op2)) {                                        op2 = Z_REFVAL_P(op2);                                        continue;                                }                                if (Z_TYPE_P(op1) == IS_OBJECT && Z_OBJ_HANDLER_P(op1, compare)) {                                        return Z_OBJ_HANDLER_P(op1, compare)(result, op1, op2);                                } else if (Z_TYPE_P(op2) == IS_OBJECT && Z_OBJ_HANDLER_P(op2, compare)) {                                        return Z_OBJ_HANDLER_P(op2, compare)(result, op1, op2);                                }                                if (Z_TYPE_P(op1) == IS_OBJECT && Z_TYPE_P(op2) == IS_OBJECT) {                                        if (Z_OBJ_P(op1) == Z_OBJ_P(op2)) {                                                /* object handles are identical, apparently this is the same object */                                                ZVAL_LONG(result, 0);                                                return SUCCESS;                                        }                                        if (Z_OBJ_HANDLER_P(op1, compare_objects) == Z_OBJ_HANDLER_P(op2, compare_objects)) {                                                ZVAL_LONG(result, Z_OBJ_HANDLER_P(op1, compare_objects)(op1, op2));                                                return SUCCESS;                                        }                                }                                if (Z_TYPE_P(op1) == IS_OBJECT) {                                        if (Z_OBJ_HT_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;                                }                }        }}
Copy after login

3.总结

       其实一般人认为比较函数很简单:就返回-1,0,1比较可以吗,为什么写的那么长。首选,php是弱类型语言,我们不能确定我们要比较两个数 是什么类型,比如数字,数字又分整型,浮点型,长整型,字符串,空等于是就注定长PHP的2个PHP的类型的个数= 2^(PHP类型的个数)

这就是一门语言,受欢迎的语言,考虑的方面比较多,全面性,

一门语言存在时间越长越长,它的补丁就越多,这个是在实践中检验出来的!你问它为什么,有可能它也不知道,反正就这样这个问题可以避免,有可能为了环境,你肯定见过#if HAVE_STRCOLL #endif 等等,也有可能避免它用函数的bug,也有可能每次对代码的抽象的不同,反正就源码就是一个难懂,难懂后面你可以看到一门语言的背后的故事,其实底层和我们做应用层原理一样,就是通过已有创造出我们想要东西,比如应用层:我们可以用php语言写一个web页面,底层只不过用c语言写一个tcp/ip 的写协议,


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Discover File Downloads in Laravel with Storage::download Discover File Downloads in Laravel with Storage::download Mar 06, 2025 am 02:22 AM

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

HTTP Method Verification in Laravel HTTP Method Verification in Laravel Mar 05, 2025 pm 04:14 PM

Laravel simplifies HTTP verb handling in incoming requests, streamlining diverse operation management within your applications. The method() and isMethod() methods efficiently identify and validate request types. This feature is crucial for building

See all articles