note 92375 added to function.list

From: Date: Tue, 21 Jul 2009 22:38:04 +0000
Subject: note 92375 added to function.list
Groups: php.notes 
Request: Send a blank email to php-notes+get-158564@lists.php.net to get a copy of this message
Example 4, above, is not exactly right. This code shows the difference experienced with PHP 5.2.6. Also, the warning about the assignment order may be confusing. If fewer arguments are contained in list() than are present in the array, the assignment to list elements is made "left-to-right" across the array, up to the number of arguments in list(). If more arguments are in list() than in the array, a Notice is generated and the excess arguments are assigned NULL. <?php // IF YOU OMIT THIS STATEMENT, $a IS NOT MADE INTO AN ARRAY // $a = array(); // $a IS NOT AN ARRAY $info = array('coffee', 'brown', 'caffeine'); list($a[0], $a[1], $a[2]) = $info; var_dump($a); // PRINTS string(3) "cbc" // IF YOU INCLUDE THIS STATEMENT, $a IS MADE INTO AN ARRAY $a = array(); $info = array('coffee', 'brown', 'caffeine'); list($a[0], $a[1], $a[2]) = $info; var_dump($a); // PRINTS array(3) { [2]=> string(8) "caffeine" [1]=> string(5) "brown" [0]=> string(6) "coffee" } // IF YOU USE FEWER ARGUMENTS FOR list() THE ASSIGNMENT IS LEFT-TO-RIGHT $a = array(); $info = array('coffee', 'brown', 'caffeine'); list($a[0], $a[1]) = $info; var_dump($a); // PRINTS array(2) { [1]=> string(5) "brown" [0]=> string(6) "coffee" } ---- Server IP: 64.71.164.2 Probable Submitter: 74.9.128.130 ---- Manual Page -- http://www.php.net/manual/en/function.list.php Edit -- https://master.php.net/note/edit/92375 Del: integrated -- https://master.php.net/note/delete/92375/integrated Del: useless -- https://master.php.net/note/delete/92375/useless Del: bad code -- https://master.php.net/note/delete/92375/bad+code Del: spam -- https://master.php.net/note/delete/92375/spam Del: non-english -- https://master.php.net/note/delete/92375/non-english Del: in docs -- https://master.php.net/note/delete/92375/in+docs Del: other reasons-- https://master.php.net/note/delete/92375 Reject -- https://master.php.net/note/reject/92375 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158564) next »