PHP function range () to quickly create an array. We are learning the range () function to quickly create a number array from 1 to 9 :? Php $ numbersrange (); use range to directly create 1 ~ 9. an array composed of 9 numbers. we are learningFor example, the range () function can quickly create a number array from 1 to 9:
- Php
- $Numbers=Range(); // Use range to directly create 1 ~ 9. an array composed of 9 numbers. it ends with "1" and "9.
- Echo $ numbers [1]; // output the second array value created: 2; echo $ numbers [0]; then enter the first value: 0.
- ?>
Of course, range (9, 1) is used to create a number array from 9 to 1. At the same time, the PHP function range () can also create a character array from a to z:
- Php
- $Numbers=Range(A, z );
- Foreach ($ numbers as $ mychrs) // traverses the $ numbers array,
The current unit value of each loop is assigned to $ mychrs.
- Echo $ mychrs. ""; // output a B c d e f g h I
J k l m n o p q r s t u v w x y z
- ?>
// Foreach is a simple way to traverse arrays. foreach can only be used for arrays. if you try to use it for another data type or an uninitialized variable, an error will occur, it has two formats:
Foreach (array_expression as $ value) statementforeach (array_expression as $ key => $ value) statement
The first format traverses the given array_expression array. In each loop, the value of the current unit is assigned to $ value and the pointer inside the array moves forward (so the next unit will be obtained in the next loop ). In the second format, only the key names of the current unit are assigned to the variable $ key in each loop.
Note the case sensitivity when using character arrays. for example, range (A, z) and range (a, Z) are different.
The PHP function range () also has a third parameter, which is used to set the step size. for example, the array elements created by range (, 3) are: 1, 4, and 7.
For example, the range () function can quickly create a number array from 1 to 9 :? Php $ numbers = range (); // use range to directly create 1 ~ 9. an array composed of 9 numbers, starting with "1...