您的位置:首页 > PHP > PHP源码阅读笔记十九:array_file,range函

PHP源码阅读笔记十九:array_file,range函

PHP源码阅读笔记十九:array_file,range函数
array_fill
(PHP 4 >= 4.2.0, PHP 5)

array_fill — 用给定的值填充数组
说明
array array_fill ( int start_index, int num, mixed value )

array_fill() 用 value 参数的值将一个数组填充 num 个条目,键名由 start_index 参数指定的开始。注意 num 必须是一个大于零的数值,否则 PHP 会发出一条警告。

对于参数start_index,只能是字符串,整形,浮点型
其源码如下:

1
2
3
4
5
6
7
8
switch (Z_TYPE_PP(start_key)) {
case IS_STRING:
case IS_LONG:
case IS_DOUBLE:
    .......
    convert_to_long_ex(start_key);
    ......
}

程序首先赋值给return_value第一个值,然后循环num – 1次: 给这个值添加refcount,并将它添加到return_value的Hash Table中

range
(PHP 3 >= 3.0.8, PHP 4, PHP 5)

range — 建立一个包含指定范围单元的数组
说明
array range ( mixed low, mixed high [, number step] )

range() 返回数组中从 low 到 high 的单元,包括它们本身。如果 low > high,则序列将从 high 到 low。

新参数: 可选的 step 参数是 PHP 5.0.0 新加的。

如果给出了 step 的值,它将被作为单元之间的步进值。step 应该为正值。如果未指定,step 则默认为 1。

从代码可以看出,本函数仅支持字符数组,浮点数组和整形数组,并且支持递增和递减两种形式(在版本4.0.1之后才有)
以字符数组为例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 
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);
}

对于浮点型和整形的处理基本类似,只有写入Hash Table的方法不同
浮点型用的是add_next_index_double
整形用的是add_next_index_long

EOF

本文地址:PHP源码阅读笔记十九:array_file,range函    文章出处:PHP源码阅读,PHP设计模式,PHP学习笔记,项目管理-胖胖的空间

转载请以链接形式注明原始出处和作者,谢绝不尊重版权者抄袭!

暂无留言我要留言

必填

必填,绝不公开

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word