Home >Backend Development >PHP Tutorial >How to quickly create an array in php (application of function range())
Of course, using range(9,1) creates 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:
//foreach is a convenient way to iterate over an array, foreach can only be used for arrays, when trying to use it with Other data types or an uninitialized variable will generate an error. It has two formats: foreach (array_expression as $value) statementforeach (array_expression as $key => $value) statement The first format iterates over the given array_expression array. Each time through the loop, the value of the current cell is assigned to $value and the pointer inside the array is moved forward one step (so the next cell will be obtained in the next loop). The second format does the same thing, except that the key name of the current cell is also assigned to the variable $key in each loopPay attention to the case 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(1,9,3) are: 1, 4, 7. The introduction to the usage of the PHP array function range() is complete. I wish you all good luck in learning. |