<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:series="http://unfoldingneurons.com/"
		>
<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>
	<lastBuildDate>Wed, 25 Aug 2010 05:58:24 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Susenjit Chanda</title>
		<link>http://blog.ninedays.org/2007/12/02/swapping-two-variables-in-one-line-with-php/comment-page-1/#comment-2447</link>
		<dc:creator>Susenjit Chanda</dc:creator>
		<pubDate>Thu, 06 Aug 2009 13:06:08 +0000</pubDate>
		<guid isPermaLink="false">http://blog.ninedays.org/2007/12/02/swapping-two-variables-in-one-line-with-php/#comment-2447</guid>
		<description>&lt;p&gt;Your one line swap is really a good code. Though memory usage is not so much important these days but the approach is effective if you wanna do this in a single line.  Thanx for the code.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Your one line swap is really a good code. Though memory usage is not so much important these days but the approach is effective if you wanna do this in a single line.  Thanx for the code.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Terri Ann</title>
		<link>http://blog.ninedays.org/2007/12/02/swapping-two-variables-in-one-line-with-php/comment-page-1/#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&#039;t really put as much thought into memory usage as it&#039;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&#8217;t really put as much thought into memory usage as it&#8217;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-page-1/#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&#039;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&#039;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&#039;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&#039;ve gone from a method that requires no extra memory, to a method that could arguably consume an unlimited amount extra - obviously that&#039;s not going to happen with a simple two variable swap, but the potential is there.  That said, you&#039;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 &#8211; 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&#8217;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 &#8211; you&#8217;re creating an array, which <strong>at best</strong> takes twice as much space as a single temp var, but you&#8217;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&#8217;ve gone from a method that requires no extra memory, to a method that could arguably consume an unlimited amount extra &#8211; obviously that&#8217;s not going to happen with a simple two variable swap, but the potential is there.  That said, you&#8217;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-page-1/#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 (&amp;$x, &amp;$y) {
  $x ^= $y ^= $x ^= $y;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This means you don&#039;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&#8217;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>
