This article mainly introduces the Nineteenth Brief Talk on PHP Source Code: Regarding the array_file and range functions, it has certain reference value. Now I share it with you. Friends in need can refer to it.
Brief Talk on PHP Source code 19: About array_file, range function
array_fill
(PHP 4 >= 4.2.0, PHP 5)
array_fill — Fill the array with the given value
Description
array array_fill (int start_index, int num, mixed value)
array_fill() Fills an array with num entries using the value of the value parameter, starting with the key name specified by the start_index parameter. Note that num must be a number greater than zero, otherwise PHP will issue a warning.
For the parameter start_index, it can only be string, integer, and floating point type.
The source code is as follows:
switch (Z_TYPE_PP(start_key)) {case IS_STRING:case IS_LONG:case IS_DOUBLE: ....... convert_to_long_ex(start_key); ......}
The program first assigns the first value to return_value, and then loops num – 1 Times: Add refcount to this value and add it to the Hash Table of return_value
range
(PHP 3 >= 3.0.8, PHP 4, PHP 5)
range - Create an array containing cells in the specified range
Description
array range (mixed low, mixed high [, number step])
range() returns the cells from low to high in the array, Including themselves. If low > high, the sequence will go from high to low.
New parameters: The optional step parameter is newly added in PHP 5.0.0.
If the value of step is given, it will be used as the step value between units. step should be positive. If not specified, step defaults to 1.
As can be seen from the code, this function only supports character arrays, floating point arrays and integer arrays, and supports both increasing and decreasing forms (only available after version 4.0.1)
Use character arrays For example:
if (Z_TYPE_P(zlow) == IS_STRING && Z_TYPE_P(zhigh) == IS_STRING && Z_STRLEN_P(zlow) >= 1 && Z_STRLEN_P(zhigh) >= 1) { int type1, type2; unsigned char *low, *high; long lstep = (long) step; type1 = is_numeric_string(Z_STRVAL_P(zlow), Z_STRLEN_P(zlow), NULL, NULL, 0); type2 = is_numeric_string(Z_STRVAL_P(zhigh), Z_STRLEN_P(zhigh), NULL, NULL, 0); if (type1 == IS_DOUBLE || type2 == IS_DOUBLE || is_step_double) { goto double_str; } else if (type1 == IS_LONG || type2 == IS_LONG) { goto long_str; } convert_to_string(zlow); // 转化为字符串,此函数的实现在zend_operators.c的536行:ZEND_API void _convert_to_string(zval *op ZEND_FILE_LINE_DC) convert_to_string(zhigh); low = (unsigned char *)Z_STRVAL_P(zlow); // 当所给字符串长度大于1时,取第一个字符 high = (unsigned char *)Z_STRVAL_P(zhigh); if (*low > *high) { // 递减数组 if (lstep <= 0) { err = 1; goto err; } for (; *low >= *high; (*low) -= (unsigned int)lstep) { add_next_index_stringl(return_value, low, 1, 1); if (((signed int)*low - lstep) < 0) { break; } } } else if (*high > *low) { // 递增数组 if (lstep <= 0) { err = 1; goto err; } for (; *low <= *high; (*low) += (unsigned int)lstep) { add_next_index_stringl(return_value, low, 1, 1); if (((signed int)*low + lstep) > 255) { // 只支持ASCII的255个字符 break; } } } else { // 开始和结束相等,则只返回包含一个元素的数组 add_next_index_stringl(return_value, low, 1, 1);}
The processing of floating point and integer is basically similar, only the method of writing to Hash Table is different
The floating point type uses add_next_index_double
The integer type uses add_next_index_long
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
A brief discussion on PHP source code 16: About array_count_values function
The above is the detailed content of A brief discussion on PHP source code 19: About array_file, range functions. For more information, please follow other related articles on the PHP Chinese website!