- 文章来源: github.com/suhanyujie/…
- 做者:suhanyujie
- 基于PHP 7.3.3
intval ( mixed $var [, int $base = 10 ] ) : int
复制代码
$base
用的不是不少。它表明转化所使用的进制。默认是 10 进制$var1 = '123';
$var2 = '-123';
$var3 = [1, 2, ];
$var4 = [-1, 2, ];
var_dump(
intval($var1),
intval($var2),
intval($var3),
intval($var4)
);
// 输出以下:
// int(-123)
// int(1)
// int(1)
复制代码
php-7.3.3/ext/standard/type.c
中,能够点击查看PHP_FUNCTION(intval)
{
zval *num;
zend_long base = 10;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ZVAL(num)
Z_PARAM_OPTIONAL Z_PARAM_LONG(base) ZEND_PARSE_PARAMETERS_END();
if (Z_TYPE_P(num) != IS_STRING || base == 10) {
RETVAL_LONG(zval_get_long(num));
return;
}
if (base == 0 || base == 2) {
char *strval = Z_STRVAL_P(num);
size_t strlen = Z_STRLEN_P(num);
while (isspace(*strval) && strlen) {
strval++;
strlen--;
}
/* Length of 3+ covers "0b#" and "-0b" (which results in 0) */
if (strlen > 2) {
int offset = 0;
if (strval[0] == '-' || strval[0] == '+') {
offset = 1;
}
if (strval[offset] == '0' && (strval[offset + 1] == 'b' || strval[offset + 1] == 'B')) {
char *tmpval;
strlen -= 2; /* Removing "0b" */
tmpval = emalloc(strlen + 1);
/* Place the unary symbol at pos 0 if there was one */
if (offset) {
tmpval[0] = strval[0];
}
/* Copy the data from after "0b" to the end of the buffer */
memcpy(tmpval + offset, strval + offset + 2, strlen - offset);
tmpval[strlen] = 0;
RETVAL_LONG(ZEND_STRTOL(tmpval, NULL, 2));
efree(tmpval);
return;
}
}
}
RETVAL_LONG(ZEND_STRTOL(Z_STRVAL_P(num), NULL, base));
}
复制代码
$var
变量类型是 mixed
,这也就意味着,输入参数能够是 PHP 中的任意一种类型,包括整形、字符串、数组、对象等。所以,在源码中直接使用 zval 接收输入参数 zval *num;
if (Z_TYPE_P(num) != IS_STRING || base == 10) {
RETVAL_LONG(zval_get_long(num));
return;
}
static zend_always_inline zend_long zval_get_long(zval *op) {
return EXPECTED(Z_TYPE_P(op) == IS_LONG) ? Z_LVAL_P(op) : zval_get_long_func(op);
}
ZEND_API zend_long ZEND_FASTCALL zval_get_long_func(zval *op) /* {{{ */ {
return _zval_get_long_func_ex(op, 1);
}
复制代码
_zval_get_long_func_ex(op, 1);
。在这个函数中,处理了各类 PHP 用户态参数类型的状况:switch (Z_TYPE_P(op)) {
case IS_UNDEF:
case IS_NULL:
case IS_FALSE:
return 0;
case IS_TRUE:
return 1;
case IS_RESOURCE:
return Z_RES_HANDLE_P(op);
case IS_LONG:
return Z_LVAL_P(op);
case IS_DOUBLE:
return zend_dval_to_lval(Z_DVAL_P(op));
case IS_STRING:
// 略 ……
case IS_ARRAY:
return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1 : 0;
case IS_OBJECT:
// 略 ……
case IS_REFERENCE:
op = Z_REFVAL_P(op);
goto try_again;
EMPTY_SWITCH_DEFAULT_CASE()
}
复制代码
经过 switch 语句的不一样分支对不一样类型作了各类不一样的处理:php
按照本文的初衷,就是要了解一下如何将字符串转化为整形数据,所以咱们着重看字符串的状况:git
{
zend_uchar type;
zend_long lval;
double dval;
if (0 == (type = is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &lval, &dval, silent ? 1 : -1))) {
if (!silent) {
zend_error(E_WARNING, "A non-numeric value encountered");
}
return 0;
} else if (EXPECTED(type == IS_LONG)) {
return lval;
} else {
/* Previously we used strtol here, not is_numeric_string,
* and strtol gives you LONG_MAX/_MIN on overflow.
* We use use saturating conversion to emulate strtol()'s * behaviour. */ return zend_dval_to_lval_cap(dval); } } 复制代码
static zend_always_inline zend_uchar is_numeric_string(const char *str, size_t length, zend_long *lval, double *dval, int allow_errors) {
return is_numeric_string_ex(str, length, lval, dval, allow_errors, NULL);
}
static zend_always_inline zend_uchar is_numeric_string_ex(const char *str, size_t length, zend_long *lval, double *dval, int allow_errors, int *oflow_info) {
if (*str > '9') {
return 0;
}
return _is_numeric_string_ex(str, length, lval, dval, allow_errors, oflow_info);
}
ZEND_API zend_uchar ZEND_FASTCALL _is_numeric_string_ex(const char *str, size_t length, zend_long *lval, double *dval, int allow_errors, int *oflow_info) { // ... }
复制代码
is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &lval, &dval, silent ? 1 : -1)
背后的函数调用,也就是函数 _is_numeric_string_ex
+/-
符号'0'
字符,好比字符串 '001a'
,转换为整形后,就是 1
,去除了前面的 '0'
字符'0'-'9'
的字符while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r' || *str == '\v' || *str == '\f') {
str++;
length--;
}
复制代码
\n
、\t
、\r
这几个用的多一些。\v
是指竖向跳格;\f
是换页符。针对这种空白符,不作处理,选择跳过。而后使用指针运算 str++
指向下一个字符+
号是能够省略的:if (*ptr == '-') {
neg = 1;
ptr++;
} else if (*ptr == '+') {
ptr++;
}
复制代码
while (*ptr == '0') {
ptr++;
}
复制代码
处理完以上的 3 种状况后,就会对接下里的字符逐个转换为整数。因为最早遍历到的字符数字是处于高位的,因此在计算下一个字符前,须要对以前的数值 *10
操做。举例说明:github
231aa
,遍历到第一个字符 '2'
时,将其做为临时值存储到变量 tmp 中'3'
,须要 *10
,也就是 tmp * 10 + 3
,此时 tmp 值为 23'1'
,须要 tmp * 10 + 1
,此时 tmp 值为 231。所以,源码中判断字符是不是数字字符:ZEND_IS_DIGIT(*ptr)
,是的话则按照上述方式计算算法
- ZEND_IS_DIGIT 宏的实现是
((c) >= '0' && (c) <= '9')
,位于'0'
和'9'
之间的字符就是咱们须要找的数字字符。
_is_numeric_string_ex
函数在底层会被多种 PHP 函数调用,包括 floatval
。若是在遍历字符串的字符时,遇到小数点该如何处理呢?我的观点看,因为咱们要实现的是 intval
函数,因此我以为遇到小数点时,能够将其看成非数字字符来处理。例如 "3.14abc"
字符串,intval 以后就直接是 3。然而实际上,_is_numeric_string_ex
的实现不是这样的,由于它是一个通用函数。在遇到小数点时,有一些特殊处理:process_double
:process_double:
type = IS_DOUBLE;
/* If there's a dval, do the conversion; else continue checking * the digits if we need to check for a full match */
if (dval) {
local_dval = zend_strtod(str, &ptr);
} else if (allow_errors != 1 && dp_or_e != -1) {
dp_or_e = (*ptr++ == '.') ? 1 : 2;
goto check_digits;
}
复制代码
_is_numeric_string_ex
函数最后会将获得的浮点数返回:if (dval) {
*dval = local_dval;
}
return IS_DOUBLE;
复制代码
dval
指针。并将数据标识 IS_DOUBLE
返回。_zval_get_long_func_ex
继续执行,也就是 return zend_dval_to_lval_cap(dval);
。该函数定义以下:static zend_always_inline zend_long zend_dval_to_lval_cap(double d)
{
if (UNEXPECTED(!zend_finite(d)) || UNEXPECTED(zend_isnan(d))) {
return 0;
} else if (!ZEND_DOUBLE_FITS_LONG(d)) {
return (d > 0 ? ZEND_LONG_MAX : ZEND_LONG_MIN);
}
return (zend_long)d;
}
复制代码
(zend_long)d
。