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.
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.
a =1 b =2 c =3 a*b*c =6
Program 2: Program to demonstrate the runtime error of list() function.
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.
a = 3