PHP: list() function
PHP: Assign variables as if they were an array
The list() function is used to assign values to a list of variables. Like array(), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation.
Version:
(PHP 4 and above)
Syntax:
list(varname1, varname2.... varnamen )
Parameters:
Name | Description | Required / Optional |
Type |
---|---|---|---|
varname1 | The first variable to be assigned | Required | Mixed* |
varname2..n | Next variable to be assigned | Optional | Mixed* |
*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.
Return value:
The assigned array.
Value Type: Array
Example:
<?php
$w3r1_array = array("php","javascript","asp");
list($x, $y, $z) = $w3r1_array;
echo "We have covered $x, $y and $z.";
$w3r2_array = array("php","javascript","asp");
list($x, , $z) = $w3r2_array;
echo "We have covered $x and $z and so many other topics";
?>
Output:
We have covered php, javascript and asp.We have covered php and asp and so many other topics
View the example in the browser
Practice here online :
See also
Previous: krsort
Next: natcasesort
PHP: Tips of the Day
Filters the collection using the given callback
Example:
<?php function tips_reject($items, $func) { return array_values(array_diff($items, array_filter($items, $func))); } print_r(tips_reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) { return strlen($item) > 4; })); ?>
Output:
Array ( [0] => Pear [1] => Kiwi )
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises