Home Backend Development PHP Tutorial A brief discussion on PHP source code 19: About array_file, range functions

A brief discussion on PHP source code 19: About array_file, range functions

Jun 28, 2018 pm 05:41 PM
range function

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);
    ......}
Copy after login

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);}
Copy after login

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 18: About array_diff_key, array_diff_assoc, array_udiff_assoc Function

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Basic usage of range function in Python Basic usage of range function in Python Jan 26, 2024 pm 03:17 PM

Range() in Python is a built-in function used to generate an integer sequence. Its basic syntax is "range(start, stop[, step])", where start represents the starting value of the sequence (can be omitted, the default is 0 ), stop represents the end value of the sequence (must be specified), step represents the step size between two adjacent numbers in the sequence (can be omitted, default is 1).

Introduction to Python functions: introduction and examples of range function Introduction to Python functions: introduction and examples of range function Nov 04, 2023 am 10:10 AM

Introduction to Python functions: Introduction and examples of range functions Python is a high-level programming language widely used in various fields. It is easy to learn and has a rich built-in function library. Among them, the range function is one of the commonly used built-in functions in Python. This article will introduce the function and usage of the range function in detail, and demonstrate its specific application through examples. The range function is a function used to generate an integer sequence. It accepts three parameters, which are the starting value (

PHP source code running problem: index error solution PHP source code running problem: index error solution Mar 09, 2024 pm 09:24 PM

PHP source code running problem: Index error resolution requires specific code examples. PHP is a widely used server-side scripting language that is often used to develop dynamic websites and web applications. However, sometimes you will encounter various problems when running PHP source code, among which "index error" is a common situation. This article will introduce some common causes and solutions of index errors, and provide specific code examples to help readers better deal with such problems. Problem Description: When running a PHP program

What is php source code What is php source code Oct 11, 2019 am 09:35 AM

PHP source code refers to PHP source code. Source code is the basis of programs and websites, and PHP, the "hypertext preprocessor", is a general open source scripting language.

Use Python's range() function to generate a sequence of numbers in a specified range Use Python's range() function to generate a sequence of numbers in a specified range Aug 22, 2023 pm 05:03 PM

Use Python's range() function to generate a sequence of numbers within a specified range. In Python programming, a very common need is to generate a sequence of numbers. These numbers can be used for various operations such as iteration, looping, indexing, etc. To meet this need, Python provides a very convenient built-in function range(). The syntax of the range() function is as follows: range(start,stop,step) where start represents the starting value (optional, default is 0),

Introduction to Python functions: functions and usage examples of the range function Introduction to Python functions: functions and usage examples of the range function Nov 03, 2023 pm 06:11 PM

Introduction to Python functions: Functions and usage examples of the range function Python is a beautiful, easy-to-read, and easy-to-write object-oriented programming language with a rich and powerful function library. Among them, the range() function is one of Python's built-in functions and is often used to generate a series of numbers. The common form is: range(start,stop[,step]). Function of the range function: The range() function can be used to generate an integer sequence, the general form is range(

How to use the range() function to generate an integer sequence in Python 2.x How to use the range() function to generate an integer sequence in Python 2.x Jul 30, 2023 pm 02:41 PM

Python is a powerful programming language that provides many convenient tools and functions to help us write code more efficiently. One of the commonly used functions is the range() function, which is used to generate a sequence of integers, which is very convenient for us to perform loop iteration or generate a list. This article will focus on how to use the range() function in Python2.x to generate an integer sequence and give some code examples. range() function in Python2.x

range() function in PHP range() function in PHP Aug 25, 2023 pm 12:01 PM

The range() function creates an array containing a range of elements. It returns the elements from start to end. Syntax range(start, end, step) Parameters start-the first value end-the last value step-the incremental return value in the range The range() function returns the elements from start to end. Example Below is an example - LiveDemo<?php$number=range(0,12,3);print_r($number);?>Output Following is the output -Array([0]=>0[1]=>3[ 2]=>6[3]=>9

See all articles