1. 隐式类型转换
例如
在这里 $a 就被隐式的转化成了字符串,源码实现如下
if (unexpected(z_type_p(op1) != is_string)) {if (z_isref_p(op1)) { op1 = z_refval_p(op1);if (z_type_p(op1) == is_string) break;}zend_try_binary_object_operation(zend_concat, concat_function);use_copy1 = zend_make_printable_zval(op1, &op1_copy);
2. 显式类型转换
例如
此时$double被强制转化为整形,该函数的实现是由源码中convert_to一系列方法实现的。
例如:zend_api void zend_fastcall _convert_to_string(zval *op zend_file_line_dc)
除此之外,还可以用
bool settype ( mixed &$var , string $type )函数来实现变量类型转化,源码实现如下:
php_function(settype){ zval *var; char *type; size_t type_len = 0; if (zend_parse_parameters(zend_num_args(), zs, &var, &type, &type_len) == failure) { return; } zval_deref(var); if (!strcasecmp(type, integer)) { convert_to_long(var); } else if (!strcasecmp(type, int)) { convert_to_long(var); } else if (!strcasecmp(type, float)) { convert_to_double(var); } else if (!strcasecmp(type, double)) { /* deprecated */ convert_to_double(var); } else if (!strcasecmp(type, string)) { convert_to_string(var); } else if (!strcasecmp(type, array)) { convert_to_array(var); } else if (!strcasecmp(type, object)) { convert_to_object(var); } else if (!strcasecmp(type, bool)) { convert_to_boolean(var); } else if (!strcasecmp(type, boolean)) { convert_to_boolean(var); } else if (!strcasecmp(type, null)) { convert_to_null(var); } else if (!strcasecmp(type, resource)) { php_error_docref(null, e_warning, cannot convert to resource type); return_false; } else { php_error_docref(null, e_warning, invalid type); return_false; } retval_true;}
版权声明:本文为博主原创文章,未经博主允许不得转载。