<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Swapping Two Variables in One Line With PHP</title>
	<atom:link href="http://blog.ninedays.org/2007/12/02/swapping-two-variables-in-one-line-with-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ninedays.org/2007/12/02/swapping-two-variables-in-one-line-with-php/</link>
	<description>web development, photography, tutorials and adventure</description>
	<pubDate>Tue, 06 Jan 2009 12:28:52 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
		<item>
		<title>By: Terri Ann</title>
		<link>http://blog.ninedays.org/2007/12/02/swapping-two-variables-in-one-line-with-php/#comment-1428</link>
		<dc:creator>Terri Ann</dc:creator>
		<pubDate>Fri, 24 Oct 2008 23:50:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.ninedays.org/2007/12/02/swapping-two-variables-in-one-line-with-php/#comment-1428</guid>
		<description>&lt;p&gt;@aquaricat This post was mostly about exploring different approaches and different ways to accomplish the same task.  You made an excellent point and I greatly appreciate you contributing your knowledge! I didn't really put as much thought into memory usage as it's not my area of expertise.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>@aquaricat This post was mostly about exploring different approaches and different ways to accomplish the same task.  You made an excellent point and I greatly appreciate you contributing your knowledge! I didn't really put as much thought into memory usage as it's not my area of expertise.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Aquaricat</title>
		<link>http://blog.ninedays.org/2007/12/02/swapping-two-variables-in-one-line-with-php/#comment-1425</link>
		<dc:creator>Aquaricat</dc:creator>
		<pubDate>Fri, 24 Oct 2008 18:40:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.ninedays.org/2007/12/02/swapping-two-variables-in-one-line-with-php/#comment-1425</guid>
		<description>&lt;p&gt;The trouble with `list($x,$y) = array($y,$x);` is that it completely defeats the purpose of doing a bitwise swap - the usefulness of a bitwise swap is that it saves memory.  Rather than initializing an extra variable, it swaps the two values in place.  These days, that's not all that important, but in environments where memory comes at a premium, every little bit helps.&lt;/p&gt;

&lt;p&gt;&lt;p&gt;Now, consider what happens when you use the &lt;code&gt;list($x,$y) = array($y,$x);&lt;/code&gt; method - you're creating an array, which &lt;strong&gt;at best&lt;/strong&gt; takes twice as much space as a single temp var, but you're also executing not just one, but two additional methods which are going to absorb memory.  Those in turn may contain temporary variables, which are going to absorb even more memory.&lt;/p&gt;
&lt;p&gt;In short, you've gone from a method that requires no extra memory, to a method that could arguably consume an unlimited amount extra - obviously that's not going to happen with a simple two variable swap, but the potential is there.  That said, you're adding a LOT of overhead with &lt;code&gt;list($x,$y) = array($y,$x);&lt;/code&gt;.&lt;/p&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>The trouble with `list($x,$y) = array($y,$x);` is that it completely defeats the purpose of doing a bitwise swap - the usefulness of a bitwise swap is that it saves memory.  Rather than initializing an extra variable, it swaps the two values in place.  These days, that's not all that important, but in environments where memory comes at a premium, every little bit helps.</p>

<p></p><p>Now, consider what happens when you use the <code>list($x,$y) = array($y,$x);</code> method - you're creating an array, which <strong>at best</strong> takes twice as much space as a single temp var, but you're also executing not just one, but two additional methods which are going to absorb memory.  Those in turn may contain temporary variables, which are going to absorb even more memory.</p>
<p>In short, you've gone from a method that requires no extra memory, to a method that could arguably consume an unlimited amount extra - obviously that's not going to happen with a simple two variable swap, but the potential is there.  That said, you're adding a LOT of overhead with <code>list($x,$y) = array($y,$x);</code>.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://blog.ninedays.org/2007/12/02/swapping-two-variables-in-one-line-with-php/#comment-680</link>
		<dc:creator>Matt</dc:creator>
		<pubDate>Sat, 28 Jun 2008 08:33:08 +0000</pubDate>
		<guid isPermaLink="false">http://blog.ninedays.org/2007/12/02/swapping-two-variables-in-one-line-with-php/#comment-680</guid>
		<description>&lt;pre&gt;&lt;code&gt;function swap (&#38;$x, &#38;$y) {
  $x ^= $y ^= $x ^= $y;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This means you don't have to do anything like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;list($x, $y) = swap($x, $y);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can just do:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;swap($x, $y);
&lt;/code&gt;&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<pre><code>function swap (&amp;$x, &amp;$y) {
  $x ^= $y ^= $x ^= $y;
}
</code></pre>

<p>This means you don't have to do anything like:</p>

<pre><code>list($x, $y) = swap($x, $y);
</code></pre>

<p>You can just do:</p>

<pre><code>swap($x, $y);
</code></pre>]]></content:encoded>
	</item>
</channel>
</rss>
