List () like array (), this is not the real function, but the language structure . list () assigns a set of variables in one-step operation. Example:
<?php
$a =array (a);
$b =array (' x ', ' y ', ' z ');
List ($b, $a) = $a;
echo $a, ' ~ ~ ~ ', $b;
Analysis: At this point, first $a= $a [1], $b = $a [0],list () is assigned from left to right, the assignment from right to left, so at this point $a=2, $a this time the value of the integer int is stored, so $a[0] is equal to NULL, then $a[0] assignment to $b
Results: 2~~~
?>
<?php
$a =array (' 1 ', ' 2 ', ' 3 ');
$b =array (' x ', ' y ', ' z ');
List ($b, $a) = $a;
echo $a, ' ~ ~ ~ ', $b;
Analysis: At this point, first $a= $a [1], $b = $a [0],list () is assigned from left to right, the assignment from right to left, so at this time $a= ' 2 ', $a this time to store a string type of value, and the string can be considered an indexed array, so $a[0]= ' 2 ', and then the $a [0] assign to $b
Results: 2~~~2
?>
<?php
Exchange values of 2 variables without using the third variable
$a = 3;
$b = 9;
List ($b, $a) =array ($a, $b);
echo $a, ' ~ ~ ', $b;//The result is: 9~~~3
?>
<?php
$arr =array (' 1 ' = ' A ', ' 3 ' = ' B ', ' 2 ' = ' C ');
List ($a,, $c) = $arr;
echo $a, ' ~ ~ ~ ', $c;
Analysis: Assign the corresponding value first, $a = $arr [0], $c = $arr [2], and then assign the value $c=c, $a =null
Results: ~~~c
?>
Use of list () in PHP