Within the PHP programming language, we're already familiar with the dollar sign symbol in PHP being used as the signal to the parser that following the dollar sign we want to create a useable variable, as well as use the double dollar sign to dynamically name variables at compile time. But we can also create a list of variables as if they were an array, using the list function in PHP.
Within the PHP world, although "list" is generally referenced as a function, is rather a language construct and therefore not really a function.
How to use the list function in PHP
Using the list function is extremely straightforward. To begin, you can pass a mixed amount of variables to it, and it will output an array. The purpose of list() is to assign a list of variables in a single operation keeping the code easier to read and avoiding repeating similar code snippets. Also, it can be useful when pulling data out of a database and you want each column assigned to a variable of which to use later in the code.
$data = ['our string', '123'];
list($test, $numbers) = $data;
# Then we can do the following
# Outputs: our string
echo $test;
The syntax of a list may appear slightly different from what you normally work with in PHP, as the following expects the "=" token. Without this token, PHP will trigger a syntax error.
# Triggers: Parse error: syntax error, unexpected token ")", expecting "="
var_dump(list($string, $numbers));
The list function does not work with strings, and outputting a created variable using strings with a list will result in null values.
list($foo) = "bar";
# Outputs: NULL
var_dump($bar);
List overcomes common repeatable issues. Below we have an example of what the list tries to solve. Manually assigning "foo" and "bar", can be replaced by using a single-line list.
$data = [0 => 'foo', 1 => 'bar'];
# This two-line section
$foo = $data[0];
$bar = $data[1];
# Can be replaced into this single line bit of code
list($foo, $bar) = $data;
It is also possible to nest lists inside each other, as the following example shows. Within the main list it also contains another list, however is uncommon as it becomes harder to read. If you are required to do this, you might want to consider a different, cleaner approach. Having the variables already assigned prior to this might be a good first step, however, it does show the possible feature of the list function in PHP.
list($foo, list($bar, $foobar)) = ['a', ['b', 'c']];
The order in which the array is consumed by the list function and how it's defined is also irrelevant, for example, the array could be unsorted.
# Unsorted array
$array = [2,7,3,0];
Another common use of list in PHP is within for each loops. Here is an example of a foreach loop, looping each item of an array and putting the first and second items of each array element through list. Because of the use of list, we're assigning two new variables, "$item and $type". Because of this, we're able to use them in the scope of the for each. This would assume the array coming in has two elements, and if for some reason it didn't, it would trigger an undefined array key error.
$array = [
['Letters', 'strings'],
['Numbers', 'integers']
];
foreach ($array as list($item, $type)) {
echo "$item are $type\n";
}
# Outputs
Letters are strings
Numbers are integers
It is also possible to use the list function in the context of a function. If you require to assign data to variables, but the data you need is inside a function, you can call that function directly and assign its output to the list function. Take the below example, where the customer's name is found within the "getCustomerName" function which returns an array of two string elements. We can then assign each item to the list function to use later on. Remember this would be subjected to a similar warning as above where one or more items may not exist. It's always a good idea (if taking this example code) to add the appropriate error-checking and handling code.
function getCustomerName() {
return ['Foo', 'Bar'];
}
list($firstName, $lastName) = getCustomerName();
echo "Customer name: $firstName $lastName";
Conclusion
Using the list() function in PHP is a great way to batch-create variables from a source, maybe MySQL database or decoded JSON string, using one simple line.
- Remember not to pass a string, as this doesn't work
- List() expressions within the function cannot be completely empty, (empty array)
- It is possible to nest a list within itself
- The order in which the indices of the array are consumed is irrelevant