Intval in PHP kernel function research [original] intval function by the way. this function seems to be the most commonly used function to forcibly convert a POST or GET parameter to the int type,
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.
| 123456789101112131415161718 |
Classa {public $ B; functionc () {}}$ a = newa (); echointval ($ a); // output 1. $ a = array ('a' => 1, 'B' => 2); echointval ($ a); // output 1 $ a = false; echointval ($ a); // output 0 $ a = true; echointval ($ a); // output 1 $ a = 1.9; echointval ($ ); // output 1 $ a = null; echointval ($ a); // output 0 |
OK. check its definition.
It is also defined in the ext/standard/type. c file.
| 12345678910111213141516171819202122232425262728 |
PHP_FUNCTION (intval) {zval ** num; longarg_base; intbase; switch (ZEND_NUM_ARGS () {case1: if (convert (ZEND_NUM_ARGS () TSRMLS_CC, "Z", & num) = FAILURE) {return;} base = 10; break; case2: 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.
| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
Values (zval * op, intbase)/* {*/{longtmp; switch (Z_TYPE_P (op) {caseIS_NULL: Z_LVAL_P (op) = 0; break; caseIS_RESOURCE: {TSRMLS_FETCH (); zend_list_delete (Z_LVAL_P (op);}/* break missing intentionally */caseIS_BOOL: caseIS_LONG: break; caseIS_DOUBLE: Lead (op) = zend_dval_to_lval (Z_DVAL_P (op); break; caseIS_STRING: {char * strval = Z_STRVAL_P (op); Z_LVAL_P (op) = str Tol (strval, NULL, base); STR_FREE (strval);} break; caseIS_ARRAY: tmp = (zend_hash_num_elements (Z_ARRVAL_P (op ))? 1:0); zval_dtor (op); Z_LVAL_P (op) = tmp; break; caseIS_OBJECT: {intretval = 1; TSRMLS_FETCH (); convert (op, IS_LONG, convert_to_long ); if (Z_TYPE_P (op) = IS_LONG) {return;} zend_error (E_NOTICE, "Object of class % s cocould 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.