PHPBuilder - Swap Values without using third variable



RSS Twitter
Snippets Algorithms

Swap Values without using third variable

by: Abdullah Ramazanoglu
|
May 31, 2007

Version: 1.1

Type: Function

Category: Algorithms

License: GNU General Public License

Description: Simple script for swaping 2 variables without using third variable.



<?

function my_swap($a,$b)
{
	echo "Before Swap<br>";
	echo "a : ".$a."<br>";
	echo "b : ".$b;

/*	$temp = $a;
	$a = $b;
	$b=$temp;*/

// Above this line is ditto original v 1.0
// Change starts (should work on any scalar types of equal length)
$a ^= $b;
$b ^= $a;
$a ^= $b;
// Change ends
// Below this line is ditto original v 1.0

	echo "<br><br>After Swap<br>";
	echo "a : ".$a."<br>";
	echo "b : ".$b;
}

my_swap(513,7231);
?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Abdullah Ramazanoglu

Comment:



Comment:

(Maximum characters: 1200). You have characters left.