- $numbers =range (1,9); Create an array of 1~9 with a range of 9 numbers directly, ending with "1" Starting with "9".
- echo $numbers [1]; The output creates a second array value: 2; echo $numbers [0]; Enter the first value: 0.
- ?>
Copy CodeOf course, using range (9,1) creates an array of numbers from 9 to 1. At the same time, the PHP function range () can also create an array of characters from A to Z:
- $numbers =range (a,z);
- foreach ($numbers as $mychrs)//traversal $numbers array,
- Each time the current cell value of the 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
- ?>
Copy Codeforeach is an easy way to iterate through an array, and foreach can only be used with arrays, which can produce an error when trying to use it for other data types or an uninitialized variable, in two formats: foreach (array_expression as $value) Statementforeach (array_expression as $key = $value) statement The first format iterates through the given array of array_expression. In each loop, the value of the current cell is assigned to the $value and the pointer inside the array is moved forward one step (so the next cell in the next loop will be taken). The second format does the same thing, except that the key name of the current cell is assigned to the variable in each loop $key Note the case when using a character array, such as range (A,Z) and range (A,Z) are not the same. The PHP function range () also has a third parameter that sets the step size, such as the array elements created by range (1,9,3): 1, 4, 7. About the use of the PHP Array function range () is finished, I wish you all the progress of learning. |