The list() function is an inbuilt function in PHP which is used to assign array values to multiple variables at a time. This function will only work on numerical arrays. When the array is assigned to multiple values, then the first element in the array is assigned to the first variable, second to the second variable, and so on, till the number of variables. The number of variables cannot exceed the length of the numerical array.
Syntax:
list($variable1, $variable2....)
Parameter: It accepts a list of variables separated by spaces. These variables are assigned values. At least one variable must be passed to the function.
Return Value: The function returns the assigned array to the multiple variables passed. If the number of variables passed is greater than the number of elements in the array, an error is thrown.
Below programs illustrate the list() function in PHP:
Program 1: Program to demonstrate the use of list() function.
<?php // PHP program to demonstrate the // use of list() function $array = array(1, 2, 3, 4); // Assign array values to variables list($a, $b, $c) = $array; // print all assigned values echo "a =", ($a), "\n"; echo " b =", ($b), "\n"; echo " c =", ($c), "\n"; // Perform multiplication of // those assigned numbers echo "a*b*c =", ($a*$b*$c); ?> |
Output:
a =1 b =2 c =3 a*b*c =6
Program 2: Program to demonstrate the runtime error of list() function.
<?php // PHP program to demonstrate the // runtime error of list() function $array = array(1, 2, 3, 4); // assign array values to variables list($a, $b, $c, $d, $e) = $array; ?> |
Output:
PHP Notice: Undefined offset: 4 in /home/619f1441636b952bbd400f1e9e8e3d0c.php on line 6
Program 3: Program to demonstrate assignment of particular index values in the array to variables.
<?php // PHP program to demonstrate assignment of // particular index values in the array to // variables. $array = array(1, 2, 3, 4); // Assign array values to variables list(, , $a) = $array; // Print all assigned values echo " a = ", ($a), "\n"; ?> |
Output:
a = 3
Reference:
http://php.net/manual/en/function.list.php
Recommended Posts:
- How to Check a Function is a Generator Function or not using JavaScript ?
- How to call a function that return another function in JavaScript ?
- Semantic-UI | List
- Bulma | List
- HTML | list Attribute
- CSS | list-style Property
- AngularJs | ng-list Directive
- SASS | List Functions
- Bootstrap 4 | List Groups
- How to create To-Do List using jQuery?
- How to define a drop-down list in HTML5 ?
- How to set vertical space between the list of items using CSS ?
- How to sort a list alphabetically using jQuery ?
- List group in bootstrap with examples
- How to get selected value in dropdown list using JavaScript ?
- How to Add Image into Dropdown List for each items ?
- HTML | <input> list Attribute
- Variable-length argument list in PHP
- Create an unordered list without any bullets using CSS
- How to hide the bullets on list for the sidebar?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ArkadyutiBanerjee

