Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

list()

Example

Assign values to multiple variables from an array-like structure.

<?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.”;
?>

Definition and Usage

The list() function assigns values to a set of variables in a single operation.

Note: Before PHP 7.1, this function only worked with numerical arrays.

Syntax

list(var1, var2, …)

Parameter Values

Parameter

Description

var1

Required. The first variable to assign a value to

var2,…

Optional. Additional variables to assign values to

Technical Details

Return Value:

Returns an array with the assigned values

PHP Version:

4+

More Examples

Example

Assign values to the first and third variables

<?php
$my_array = array(“Dog”,“Cat”,“Horse”);

list($a, , $c) = $my_array;
echo “Here I only use the $a and $c variables.”;
?>