note 113189 added to function.list
| From: | megan at voices dot com | Date: | Tue, 10 Sep 2013 15:45:59 +0000 |
| Subject: | note 113189 added to function.list | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196215@lists.php.net to get a copy of this message | ||
As noted, list() will give an error if the input array is too short. This can be avoided by
array_merge()'ing in some default values. For example:
<?php
$parameter = 'name';
list( $a, $b ) = array_merge( explode( '=', $parameter ), array( true ) );
?>
However, you will have to array_merge with an array long enough to ensure there are enough elements
(if $parameter is empty, the code above would still error).
An alternate approach would be to use array_pad on the array to ensure its length (if all the
defaults you need to add are the same).
<?php
$parameter = 'bob-12345';
list( $name, $id, $fav_color, $age ) = array_pad( explode( '-', $parameter ), 4,
'' );
var_dump($name, $id, $fav_color, $age);
/* outputs
string(3) "bob"
string(5) "12345"
string(0) ""
string(0) ""
*/
?>
----
Server IP: 69.147.83.199
Probable Submitter: 206.47.126.34
----
Manual Page -- http://php.net/manual/en/function.list.php
Edit -- https://master.php.net/note/edit/113189
Del: integrated -- https://master.php.net/note/delete/113189/integrated
Del: useless -- https://master.php.net/note/delete/113189/useless
Del: bad code -- https://master.php.net/note/delete/113189/bad+code
Del: spam -- https://master.php.net/note/delete/113189/spam
Del: non-english -- https://master.php.net/note/delete/113189/non-english
Del: in docs -- https://master.php.net/note/delete/113189/in+docs
Del: other reasons-- https://master.php.net/note/delete/113189
Reject -- https://master.php.net/note/reject/113189
Search -- https://master.php.net/manage/user-notes.php