php

PHP List() Function

The PHP array is used to store the multiple values in a variable. Sometimes, it requires storing one or more or all values of the array in one or multiple variables. This function is first introduced in PHP version 4 that is used to store the values of the numeric array only. The key values of the array are stored by this function from PHP version 7.1. The different ways of reading the array values using this function are shown in this tutorial.

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.

<?php

//Declare a string array
$str_arr = array('Welcome', 'to', 'LinuxHint');
//Assign the array values in list variables
list($val1, $val2, $val3) = $str_arr;
//Print the values of the list variables
printf("<b>The array values are:</b><br/>%s<br/>%s<br/>%s<br/>", $val1, $val2, $val3);
?>

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.

<?php

//Declare a numeric array
$num_arr = array(70, 39, 12, 82, 53);
//Assign the array values in two list variables
list($val1, $val2) = $num_arr;
//Print the values of the list variables
printf("<b>The first two array values are:</b><br/>%s<br/>%s<br/>", $val1, $val2);
?>

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.

<?php

//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.

<?php

//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.

<?php

//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.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.