PHP list() Function

In this chapter you will learn:

  1. Definition for PHP list() Function
  2. Syntax for PHP list() Function
  3. Parameter for PHP list() Function
  4. Note for PHP list() Function
  5. Example - Assign value in an array to a list of variables
  6. Example - Using the first and third variables
  7. Example - Compare the code with and without list() function
  8. Example - Use list during array loop

Definition

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

Syntax

PHP list() Function has the following syntax.

list(var1,var2...)

Parameter

ParameterIs RequiredDescription
var1Required.The first variable to assign a value to
var2,...Optional.More variables to assign values to

Note

This function only works on numerical arrays.

Example 1

Assign value in an array to a list of variables


<?php/*from   j a va2s .  c  o  m*/
$my_array = array("A","B","C");

list($a, $b, $c) = $my_array;
echo "I have several animals, a $a, a $b and a $c.";
?>

The code above generates the following result.

Example 2

Using the first and third variables:


<?php/* j ava 2  s. c  o m*/
$my_array = array("A","B","C");

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

The code above generates the following result.

Example 3

list() pulls out the values of an array into separate variables.


<?PHP/*  j a  va 2s.c  o  m*/
         $myBook = array( "Learn PHP from java2s.com", "java2s.com", 2000 ); 

         $title = $myBook[0]; 
         $author = $myBook[1]; 
         $pubYear = $myBook[2]; 

         echo $title . "\n";    
         echo $author . "\n";   
         echo $pubYear . "\n";  
?>

You use it as follows:


<?PHP/*from ja  va 2  s . c  o m*/
         $myBook = array( "Learn PHP from java2s.com", "java2s.com", 2000 ); 
         list( $title, $author, $pubYear ) = $myBook; 

         echo $title . "\n";    
         echo $author . "\n";   
         echo $pubYear . "\n";  
?>

list() only works with indexed arrays, and it assumes the elements are indexed consecutively starting from zero so the first element has an index of 0 , the second has an index of 1 , and so on.

Example 4

A classic use of list() is with functions such as each() that return an indexed array of values. For example:


<?PHP//from jav  a2 s . c o m
    $myBook = array( "title" =>  "Learn PHP from java2s.com", 
                     "author" =>  "java2s.com", 
                     "pubYear" =>  2000 ); 
    while ( list( $key, $value ) = each( $myBook ) ) { 
      echo "Key: $key \n"; 
      echo "Value: $value"; 
    }     
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP natcasesort() Function
  2. Syntax for PHP natcasesort() Function
  3. Parameter for PHP natcasesort() Function
  4. Return for PHP natcasesort() Function
  5. Note for PHP natcasesort() Function
  6. Example - sorts an array by using a "natural order" algorithm
Home » PHP Tutorial » PHP Array Functions
PHP array() function
PHP array_change_key_case() function
PHP array_chunk() function
PHP array_combine() function
PHP array_count_values() function
PHP array_diff() function
PHP array_diff_assoc() Function
PHP array_diff_key() function
PHP array_diff_uassoc() function
PHP array_diff_ukey() function
PHP array_fill() function
PHP array_fill_keys() function
PHP array_filter() function
PHP array_flip() function
PHP array_intersect() Function
PHP array_intersect_assoc() Function
PHP array_intersect_key() function
PHP array_intersect_uassoc() Function
PHP array_intersect_ukey() Function
PHP array_key_exists() Function
PHP array_keys() function
PHP array_map() Function
PHP array_merge() function
PHP array_merge_recursive() Function
PHP array_multisort() Function
PHP array_pad() Function
PHP array_pop() function
PHP array_product() Function
PHP array_push() function
PHP array_rand() function
PHP array_reduce() Function
PHP array_replace() Function
PHP array_replace_recursive() Function
PHP array_reverse() Function
PHP array_search() Function
PHP array_shift() Function
PHP array_slice() Function
PHP array_splice() Function
PHP array_sum() Function
PHP array_udiff() Function
PHP array_udiff_assoc() Function
PHP array_udiff_uassoc() Function
PHP array_uintersect() Function
PHP array_uintersect_assoc() Function
PHP array_uintersect_uassoc() Function
PHP array_unique() Function
PHP array_unshift() Function
PHP array_values() Function
PHP array_walk() Function
PHP array_walk_recursive() Function
PHP arsort() Function
PHP asort() Function
PHP compact() Function
PHP count() Function
PHP current() Function
PHP each() Function
PHP end() Function
PHP explode() Function
PHP extract() Function
PHP implode() Function
PHP in_array() Function
PHP key() Function
PHP krsort() Function
PHP ksort() Function
PHP list() Function
PHP natcasesort() Function
PHP natsort() Function
PHP next() Function
PHP pos() Function
PHP prev() Function
PHP range() Function
PHP reset() Function
PHP rsort() Function
PHP shuffle() Function
PHP sizeof() Function
PHP sort() Function
PHP uasort() Function
PHP uksort() Function
PHP usort() Function