PHP » GoLang |
login |
register |
about
|
list(PHP 4, PHP 5, PHP 7) list — Assign variables as if they were an array DescriptionLike array(), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation.
Warning
In PHP 5, list() assigns the values starting with the right-most parameter. In PHP 7, list() starts with the left-most parameter. If you are using plain variables, you don't have to worry about this. But if you are using arrays with indices you usually expect the order of the indices in the array the same you wrote in the list() from left to right, which is not the case in PHP 5, as it's assigned in the reverse order. Generally speaking, it is advisable to avoid relying on a specific order of operation, as this may change again in the future. Warning
Modification of the array during list() execution (e.g. using list($a, $b) = $b) results in undefined behavior. Parameters
Return ValuesReturns the assigned array. Changelog
Examples
Example #1 list() examples
<?php
Example #2 An example use of list()
<table>
Example #3 Using nested list()
<?php int(1) int(2) int(3)
Example #4 Using list() with array indices
<?php Gives the following output (note the order of the elements compared in which order they were written in the list() syntax): Output of the above example in PHP 7: array(3) { [0]=> string(6) "coffee" [1]=> string(5) "brown" [2]=> string(8) "caffeine" } Output of the above example in PHP 5: array(3) { [2]=> string(8) "caffeine" [1]=> string(5) "brown" [0]=> string(6) "coffee" }
Example #5 list() and order of index definitions The order in which the indices of the array to be consumed by list() are defined is irrelevant.
<?php Gives the following output (note the order of the elements compared in which order they were written in the list() syntax): array(4) { [2]=> string(1) "a" ["foo"]=> string(1) "b" [0]=> string(1) "c" [1]=> string(1) "d" } string(1) "c" string(1) "d" string(1) "a" |
more
Recently updated
more
Most requested
more
Last requests
|