php tutorial - How to Create PHP Array Dynamically - php programming - learn php - php code - php script
- PHP provides some built-in array functions which help to do array operation so easily.
- The range() function is one of them. With the range() function, PHP numeric array can be created dynamically.
- The structure of the range() function is given below
- Here, the array variable $a will be numeric array whose elements start from lower limit and end at upper limit.
- Step size determines the number of array elements. So, if the lower limit is 0 and the upper limit is 100 and the step size is 10, the array elements will be calculated from the below mathematics
- Again, if the lower limit is 5 and the upper limit is 50 and the step size is 10, the elements will be
- Here, the result is a fraction number. In this case, PHP will ignore the fraction value and take the lower round figure. So, the array elements will be 5.
Sample Code
Output
- The example is showing that the array $a has been created dynamically with the range() function and the elements are being increased from lower limit to upper limit.
- But the range() function can also create an array whose elements will be decreased from upper limit to lower limit.
Output
- Here, the array elements are starting from upper limit and end with lower limit. So, with the range() function both types of array can be created.