list() function in PHP || PHP list function
×


list() function in php

0 914

PHP list() function is used to assign the values of a PHP array to a list of variables in a single step. It is an inbuilt function of PHP.

There are several conditions that are necessary for working on this function.

  • The array must be an indexed array means this function can only work for arrays that have numeric keys.
  • The number of variables must be less than or equal to the length of the PHP array.
  • This function assigns the first value of an array to the first variable and second value to the second variable and so on until the last variable.

Syntax:

list($var1,$var2,......,$varN);

here,

$var1,$var2 and $varN are PHP variables in which $var1 is mandatory and other variables are optional.


Example:

<?php
$arr1 = array("apple","orange","banana","fig",);
list($fruit1,$fruit2,$fruit3,$fruit4) = $arr1;
echo "Ram bought $fruit1,$fruit2,$fruit3 and $fruit4 from market";
?>

Output:




Share:


Leave a Reply


Comments
    Waiting for your comments