PHP Array Function : array_fill()

The array_fill() function is used to fills an array with values. The syntax would be :
 array_fill(index, number, value);
Where index denotes the first index of the returned array, number denotes the number of elements to insert, value denotes the value to use for filling the array. It returns a filled array with supplied values.
Example :
<?php

  $my_arr = array_fill(0, 4,"PHP");

  for($x = 0; $x < 4; $x++) {
    echo $my_arr[$x] . "<br/>";
  }

?>
Output :

PHP
PHP
PHP
PHP

Another example :
<?php

  $my_arr = array_fill(4, 5,"PHP");

  for($x = 4; $x < 9; $x++) {
    echo $my_arr[$x] . "<br/>";
  }

?>
Output :

PHP
PHP
PHP
PHP
PHP


Next Topic :