Syntax:
list($variable1, $variable2, …)
One or more variables can be used as the argument values of this function. The arguments are separated by a comma. One argument is mandatory for this function. The array values are assigned to the argument variables that are defined in the list() function. An error is returned if the number of arguments that are defined in this function is more than the array values.
Different Examples of List() Functions
The different uses of the list() function are shown in this part of the tutorial using multiple examples.
Example 1: Print All Array Values
Create a PHP file with the following script stores all values of the array into the variables using the list() function. Here, the array contains three string values. These values are stored into three variables and these variables are printed later.
Output:
The following output appears after executing the previous script:
Example 2: Print the First Two Values of the Array
Sometimes, it requires printing a particular number of elements from the beginning of the array. This task can be done using the list() function. Create a PHP file with the following script that reads the first two values of the array into two variables using the list() function. The array contains 5 values and the first two values of the array are stored in two variables that are printed later.
Output:
The following output appears after executing the previous script. The first and the second values of the array are 70 and 39:
Example 3: Print the First and Third Values of the Array
Sometimes, it requires printing the particular elements of the array from different positions. This task can be done using the list() function. Create a PHP file with the following script that reads the first and third values of the array into two variables using the list() function. The array contains 5 values. The first and the third values of the array are stored into two variables that are printed later.
//Declare a numeric array
$num_arr = array(70, 39, 12, 82, 53);
//Assign the first and third values of the array in two variables
error_reporting(1);list($val1, , $val3) = $num_arr;
//Print the values of the list variables
printf("<b>The first and third array values are:</b><br/>%s<br/>%s<br/>", $val1, $val3);
?>
Output:
The following output appears after executing the previous script. The first and the third values of the array are 70 and 12:
Example 4: Using More Variables in the List()
If the number of variables that are assigned in the list() function is more than the array elements, the extra variable is empty. Create a PHP file with the following script that reads all values of the array into five variables and the values of the $var1 and $var are printed later. The array contains three elements only. So, the $var4 and $var5 are empty.
//Declare a numeric array
$num_arr = array(70, 39, 12);
//Assign five variables in the list() which are greater than the array values
list($val1, $val2, $val3, $val4, $val5) = $num_arr;
//Print the values of the list variables
printf("<b>The value of the first and fifth list variables are:</b><br/>%s<br/>%s<br/>", $val1, $val5);
?>
Output:
The following output appears after executing the previous script. The value of $val1 is printed in the output:
Example 5: Use the List() with the Explode() Function
The explode() function is used to create an array from a string value based on a separator. Create a PHP file with the following script that stores the array values that are generated by the explode() function into four variables which are printed later. The explode() function that is used in the script creates an array by splitting a string based on the space.
//Assign the output of the explode() function in the list() arguments
list($val1, $val2, $val3, $val4) = explode(" ", "I like PHP Programming");
//Print the values of the list variables
printf("<b>The values of list variables are:</b><br/>%s<br/>%s<br/>%s<br/>%s<br/>", $val1, $val2, $val3, $val4);
?>
Output:
The following output appears after executing the previous script. All values of the array are printed in the output:
Conclusion
The list() is a useful function of PHP that is used to read all or the particular values of the array. The different ways of reading the array values using the list() function are shown in this tutorial through simple examples. We hope that you now understand the purpose of using the list() function after reading this tutorial.