Home > Backend Development > PHP Tutorial > Introduction to the usage of PHP range() function

Introduction to the usage of PHP range() function

PHPz
Release: 2023-06-27 17:08:01
Original
1722 people have browsed it

PHP is a very popular programming language, and developers benefit from its many built-in functions and elements. Among them, the range() function is a very useful function built into PHP, which is used to create a regular array.

This article will introduce the usage of the range() function, including the syntax, parameters, return values ​​and examples of the function. If you want to use the range() function to construct an array, this article will be very helpful to you.

The syntax of the range() function

The syntax of the range() function is:

range ($start, $end, $step);
Copy after login

Among them, $start represents the starting value of the array, $end represents the end of the array Value, $step represents the interval between each element in the array. These parameters must be of numeric type. If the $step parameter is not provided, it defaults to 1.

Note: In PHP, you can use integers or floating point numbers as parameters, but floating point numbers are rarely used as parameters.

Parameters of the range() function

The following is the parameter description of the range() function:

  1. $start (required parameter): Specify the starting value of the array . This parameter must be of numeric type and can be an integer or a floating point number.
  2. $end (required parameter): Specifies the end value of the array. This parameter must be of numeric type and can be an integer or a floating point number.
  3. $step (optional parameter): Specify the interval of the array, that is, the difference between adjacent elements in the array. This parameter must be of numeric type and can be an integer or a floating point number. If this parameter is omitted, it defaults to 1.

The return value of the range() function

The range() function will return an array, an arithmetic array from the starting value to the ending value, and the difference between its elements The value is determined by the third parameter $step. If $step is not specified, it will default to 1.

Examples of range() function

Here are some examples of using the range() function to create arrays:

  1. Creating an array of integers:
$num_array = range(0, 10);
print_r($num_array);
Copy after login

Output result:

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
    [10] => 10
)
Copy after login
  1. Create an array of floating point numbers:
$num_array = range(0, 10, 0.5);
print_r($num_array);
Copy after login

Output result:

Array
(
    [0] => 0
    [1] => 0.5
    [2] => 1
    [3] => 1.5
    [4] => 2
    [5] => 2.5
    [6] => 3
    [7] => 3.5
    [8] => 4
    [9] => 4.5
    [10] => 5
    [11] => 5.5
    [12] => 6
    [13] => 6.5
    [14] => 7
    [15] => 7.5
    [16] => 8
    [17] => 8.5
    [18] => 9
    [19] => 9.5
    [20] => 10
)
Copy after login
  1. Create a reverse order Array:
$num_array = range(10, 0, -1);
print_r($num_array);
Copy after login

Output result:

Array
(
    [0] => 10
    [1] => 9
    [2] => 8
    [3] => 7
    [4] => 6
    [5] => 5
    [6] => 4
    [7] => 3
    [8] => 2
    [9] => 1
    [10] => 0
)
Copy after login

Summary

This article introduces the usage of PHP's built-in function range(), including the function's syntax, parameters, and return value and examples. By using the range() function, you can easily create an array containing regular elements. If you want to learn more about PHP's arrays and functions, you can learn from the official documentation or other tutorials.

The above is the detailed content of Introduction to the usage of PHP range() function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template