PHP list() Function

Example

Assign values from the array to some variables:

<?php
$my_array = array("Dog","Cat","Horse");
list($a, $b, $c) = $my_array;
echo "I have several animals, a $a, a $b and a $c.";
?>

Run Example

Definition and Usage

The list() function is used to assign values to a group of variables in one operation.

Note:This function is used only for arrays with numeric indices and assumes that numeric indices start from 0.

Syntax

list(var1,var2...)
Parameter Description
var1 Required. The first variable to be assigned.
var2,... Optional. More variables to be assigned.

Description

list() function assigns values from elements of an array to a group of variables.

Note, with array() Similarly, list() is actually a language structure, not a function.

Technical Details

Return Value: Return the assigned array.
PHP Version: 4+

More Examples

Example 1

Use the first and third variables:

<?php
$my_array = array("Dog","Cat","Horse");
list($a, , $c) = $my_array;
echo "I used only $a and $c variables here.";
?>

Run Example