note 90859 added to function.list
| From: | claude dot pache at gmail dot com | Date: | Wed, 13 May 2009 10:26:40 +0000 |
| Subject: | note 90859 added to function.list | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-154537@lists.php.net to get a copy of this message | ||
A simple way to swap variables (correction of a note of mario dot mueller dot work at gmail dot com
below):
<?php
list($var1, $var2) = array($var2, $var1); // swaps the values of $var1 and $var2
?>
Note that this is not equivalent to:
<?php
$var2 = $var1; $var1 = $var2; // $var1 and $var2 get both the old value of $var1
?>
as one could fear. Indeed, the array is constructed with the values of $var1 and $var2 (and not with
the variables $var1 and $var2 themselves) before the assignment is carried out.
Similarly, it is possible to bypass the problem pointed by sasha in the previous note by providing
an expression rather than a variable on the right-hand side of the assignment operator:
<?php
$var = array ("test" ,"blah");
list ($a,$var) = $var + array();
echo $a ; // prints "test", not "b"
echo $var ; // prints "blah"
?>
----
Server IP: 89.31.146.176
Probable Submitter: 213.103.149.165
----
Manual Page -- http://www.php.net/manual/en/function.list.php
Edit -- https://master.php.net/note/edit/90859
Del: integrated -- https://master.php.net/note/delete/90859/integrated
Del: useless -- https://master.php.net/note/delete/90859/useless
Del: bad code -- https://master.php.net/note/delete/90859/bad+code
Del: spam -- https://master.php.net/note/delete/90859/spam
Del: non-english -- https://master.php.net/note/delete/90859/non-english
Del: in docs -- https://master.php.net/note/delete/90859/in+docs
Del: other reasons-- https://master.php.net/note/delete/90859
Reject -- https://master.php.net/note/reject/90859
Search -- https://master.php.net/manage/user-notes.php