Exchange Value of Variables with PHP and Perl
When exchanging the values of two variables, a third variable is generally constructed to temporarily hold one of the variables while the exchange is made. I'll show you a more elegant way.
For example, let's assume the variable $low contains the number 9 and the variable $high contains the number 4. $high should not be lower than $low. We want to exchange the values of those two numbers.
Here is one way to do it (identical coding with both Perl and PHP):
if($high < $low) { $temp = $high; $high = $low; $low = $temp; }
Here is a more elegant way with Perl.
($high,$low) = ($low,$high) if ($high < $low);
The more elegant way with PHP is similar to that provided for Perl. Here is the PHP:
if($high < $low) { list($high,$low) = array($low,$high); }
Will Bontrager