The Wayback Machine - https://web.archive.org/web/20240712094930/https://www.geeksforgeeks.org/php-list-function/
Open In App

PHP list() Function

Last Updated : 20 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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



Previous Article
Next Article

Similar Reads

How the User-Defined Function differs from Built-in Function in PHP ?
In PHP, functions are blocks of reusable code that perform specific tasks. They enhance code readability, modularity, and maintainability by encapsulating logic into named units. PHP functions can be categorized into two main types: user-defined functions and built-in functions. User-Defined FunctionsUser-defined functions are functions created by
2 min read
PHP 5 vs PHP 7
PHP is a server side scripting language designed for web development by Rasmus Lerdorf in 1994. Since its launch in 1994 PHP has become an industry standard supporting almost 80% of the websites ( 79.8% to be precise) with its closest competitor being ASP.Net at 19.8% and others like Ruby, Java trailing far behind. The PHP development team released
4 min read
PHP | Get PHP configuration information using phpinfo()
PHP provides us with a built-in function phpinfo() which gives us the details about the PHP version and PHP configuration of PHP installed in our system. To know about the Configurations and PHP version which is installed in your computer, a simple PHP script can be used. The script consists of a PHP function called "phpinfo()" which outputs inform
2 min read
How to include content of a PHP file into another PHP file ?
Including the content of a PHP file into another file reduces the complexity of code by reducing the code and improving its modularity by splitting the code into different files so that it is easy to understand and manage. There are two ways to do it by using the following PHP functions. PHP include() Function: It will show the warning if the file
2 min read
What is New in PHP Type Hinting Support in PHP 8?
PHP 8 introduced several enhancements and new features related to type hinting. That will check the code for their respective type so that we can use our code efficiently. These are the following features used in PHP Type Hitting Support: Table of Content Union TypesMixed TypeStatic Return TypeInheritance of Contravariant ParametersInheritance of P
3 min read
How to generate a drop down list of timezone using PHP ?
The timezone_identifiers_list() function is used to generate a dropdown list of timezone with PHP. This function is used to return an indexed array containing all the timezone identifiers. The datetimezone object is sent as a parameter to the timezone_identifiers_list() function and it returns an indexed array on success or False on failure. This f
3 min read
Variable-length argument list in PHP
Given a set of arguments whose length is unknown at present, we will see how a function can work with these unknown numbers of arguments whose quantity will vary depending on the requirement. We will take up each word one by one to deeply understand the topic we are dealing with. Variable: It is the number of arguments keep changing. Length: It ref
2 min read
Create a drop-down list that options fetched from a MySQL database in PHP
In many scenarios, we may need to create a dropdown input that can display all the options consistent with the current state of the database. This form of input is used many times in real life and the following examples may help to understand the same. A set of students who have unique registration numbers.A set of branch names and their branch ids
4 min read
How to Populate Dropdown List with Array Values in PHP?
We will create an array, and then populate the array elements to the dropdown list in PHP. It is a common task when you want to provide users with a selection of options. There are three approaches to achieve this, including using a foreach loop, array_map() function, and implode() function. Here, we will explore each approach with detailed explana
2 min read
PHP | imagecreatetruecolor() Function
The imagecreatetruecolor() function is an inbuilt function in PHP which is used to create a new true-color image. This function returns a blank image of the given size. Syntax: resource imagecreatetruecolor( $width, $height ) Parameters: This function accepts two parameters as mentioned above and described below: $width: This parameter is used to s
2 min read
Article Tags :
three90RightbarBannerImg