Study on PHP kernel functions

Source: Internet
Author: User

Statement: This article isSicenOriginal, all analyzed by the author one by one. If something is wrong, I hope you can enlighten me.
You are welcome to reprint it. Please indicate the source for reprinting.
Address:Http://imsiren.com/archives/520

By the way, the intval function seems to be the most commonly used function to forcibly convert a parameter to the int type during post or get,

To ensure that we are an integer when passed into SQL, of course this is only one of the usage ..
This function accepts two parameters. The first is the string to be converted, and the second is the hexadecimal number to be converted. The default value is decimal.

Let's first use PHP to check its usage.

Class A {public $ B; Function C () {}}$ A = new A (); echo intval ($ A); // Output 1. $ A = array ('A' => 1, 'B' => 2); echo intval ($ A); // output 1 $ A = false; echo intval ($ A); // output 0 $ A = true; echo intval ($ A); // output 1 $ A = 1.9; echo intval ($ ); // output 1 $ A = NULL; echo intval ($ A); // output 0

OK. Check its definition.
It is also defined in the EXT/standard/type. c file.

PHP_FUNCTION(intval){        zval **num;        long arg_base;        int base;         switch (ZEND_NUM_ARGS()) {                case 1:                        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &num) == FAILURE) {                                return;                        }                        base = 10;                        break;                 case 2:                        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zl", &num, &arg_base) == FAILURE) {                                return;                        }                        base = arg_base;                        break;                 default:                        WRONG_PARAM_COUNT;        }         RETVAL_ZVAL(*num, 1, 0);        convert_to_long_base(return_value, base);}

This function will be processed based on the number of passed parameters. The second parameter is the hexadecimal number to be converted. If it is null, the default value is 10.
Finally, convert_to_long_base (return_value, base) is called to process the input data.
This function is defined in Zend/zend_operators.c.

ZEND_API void convert_to_long_base(zval *op, int base) /* {{{ */{        long tmp;                        switch (Z_TYPE_P(op)) {                case IS_NULL:                        Z_LVAL_P(op) = 0;                        break;                case IS_RESOURCE: {                                TSRMLS_FETCH();                                 zend_list_delete(Z_LVAL_P(op));                        }                        /* break missing intentionally */                case IS_BOOL:                case IS_LONG:                        break;                case IS_DOUBLE:                        Z_LVAL_P(op) = zend_dval_to_lval(Z_DVAL_P(op));                        break;                case IS_STRING:                        {                                char *strval = Z_STRVAL_P(op);                                 Z_LVAL_P(op) = strtol(strval, NULL, base);                                STR_FREE(strval);                        }                        break;                case IS_ARRAY:                        tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0);                        zval_dtor(op);                        Z_LVAL_P(op) = tmp;                        break;                case IS_OBJECT:                            {                                int retval = 1;                                TSRMLS_FETCH();                                 convert_object_to_type(op, IS_LONG, convert_to_long);                                 if (Z_TYPE_P(op) == IS_LONG) {                                        return;                                }                                zend_error(E_NOTICE, "Object of class %s could not be converted to int", Z_OBJCE_P(op)->name);                                 zval_dtor(op);                                ZVAL_LONG(op, retval);                                return;                        }                default:                        zend_error(E_WARNING, "Cannot convert to ordinal value");                        zval_dtor(op);                        Z_LVAL_P(op) = 0;                        break;        }         Z_TYPE_P(op) = IS_LONG;}

This function first gets the type and processes it differently.

19 rows. If it is is_double type, it will be forcibly converted # define zend_dval_to_lval (D) (long) (d ))

Line 21-27 will call the C function strtol to convert it to the long type.

29-33 rows. If the value is of the array type, the number of arrays is obtained. If there is a value, the value is set to 1; otherwise, the value is 0. this is why we have n keys and values in the array, and why 1 is returned:

No matter how many key values there are in the array, as long as there is a value, it is 1.

Lines 34-49. If it is an object, it will first call convert_to_long to forcibly convert it to long. Otherwise, an exception is thrown and 1 is returned.

If no type is returned, 0 is returned.

Source: http://imsiren.com/archives/520

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.