PHP list() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, list() is a language construct that copies the values of an array into separate variables in a single line. This is called destructuring.

Instead of writing $x = $point[0]; $y = $point[1];, you write list($x, $y) = $point;. Since PHP 7.1 you can also use the short form [$x, $y] = $point;, which does exactly the same thing and is what most new code uses.

Syntax

Call list() with the values you need. Start with this line.

PHP
list($x, $y) = $point;
CallWhat it does
list($x, $y) = $point;Puts the first value in $x and the second in $y.
[$x, $y] = $point;The short form of the same thing (PHP 7.1 and later).
["id" => $id] = $row;Picks a value by its key (PHP 7.1 and later).

list() is not a normal function. It only works on the left side of an assignment. You can skip a value by leaving an empty spot, like list(, $second) = $array;.

Library declarations
PHP
list($a, $b) = $array

Parameters

ParameterDescription
varRequired. One or more variables that receive the values, in order. You can leave a spot empty to skip a value.
arrayRequired. The array on the right side of the = sign that supplies the values.

Return Value

Return ValueDescription
arrayThe assignment itself gives back the array on the right side. The real result is that the variables on the left now hold the values.

PHP Examples

Example 1 — Split an Array into Variables

list() copies each value into its own variable, in the same order as the array.

PHP
<?php
$point = [10, 20];

list($x, $y) = $point;

echo "x = ", $x, ", y = ", $y, PHP_EOL;

How It Works

  1. $point is a list with two values.
  2. list($x, $y) matches the first variable with key 0 and the second with key 1.
  3. So $x becomes 10 and $y becomes 20.
  4. echo prints both variables.

Example 2 — Short Form, Skipping and Swapping

The short form [] works the same as list(). You can also skip values and even swap two variables without a temporary variable.

PHP
<?php
[$first, , $third] = ["a", "b", "c"];
echo $first, $third, PHP_EOL;

$a = 1;
$b = 2;
[$a, $b] = [$b, $a];
echo $a, " ", $b, PHP_EOL;

How It Works

  1. The empty spot between the commas skips the second value, "b".
  2. $first receives "a" and $third receives "c".
  3. On the right, [$b, $a] builds a new array holding 2 and 1.
  4. Assigning it to [$a, $b] swaps the values, so $a is 2 and $b is 1.

Example 3 — Pick Values by Key

From PHP 7.1 you can name the keys, which is perfect for rows from a database or API result.

PHP
<?php
$row = ["id" => 7, "name" => "Asha", "city" => "Pune"];

["id" => $id, "name" => $name] = $row;

echo $id, PHP_EOL;
echo $name, PHP_EOL;

How It Works

  1. $row has three keys, but we only need two of them.
  2. "id" => $id tells PHP to put the value of the key id into $id.
  3. "name" => $name does the same for the key name.
  4. The city value is ignored, and the two variables are printed.

Frequently Asked Questions

It assigns the values of an array to several variables at once. This is called array destructuring.
No. It is a language construct, so it can only be used on the left side of an assignment. That is why it has no return value that you would normally use.
Nothing important. Since PHP 7.1, [$a, $b] = $array; does the same thing as list($a, $b) = $array;. The short form is easier to read.
Leave the spot empty, for example list(, $second) = $array; or [, $second] = $array;.
Yes, from PHP 7.1. Write list("id" => $id) = $row; or ["id" => $id] = $row;. Without keys, list() reads the numeric keys 0, 1, 2 in order.

Next: natcasesort()

natcasesort() sorts an array of strings in natural order, ignoring uppercase and lowercase.

natcasesort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful