We are learningFor example, the range () function can quickly create arrays of numbers from 1 to 9:
-
Php
- $ 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.
- ?>
Of 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:
-
Php
- $ 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
- ?>
foreach is an easy way to iterate through an array, and foreach can only be used with arrays, and when it tries to use it for other data types or an uninitialized variable, it produces an error, which has two forms:
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.
http://www.bkjia.com/PHPjc/446404.html www.bkjia.com true http://www.bkjia.com/PHPjc/446404.html techarticle we're learning. For example, the range () function can quickly create arrays of numbers from 1 to 9: PHP $ numbers = range (1,9),//Create an array of 9 numbers directly with range, start with "1" ...