<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tobias Cohen</title>
	<atom:link href="http://tobiascohen.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://tobiascohen.com</link>
	<description>Journal of a Web Developer</description>
	<lastBuildDate>Wed, 26 Aug 2009 00:46:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Making Cygwin look at your .bashrc</title>
		<link>http://tobiascohen.com/2009/08/26/making-cygwin-look-at-your-bashrc/</link>
		<comments>http://tobiascohen.com/2009/08/26/making-cygwin-look-at-your-bashrc/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 00:40:46 +0000</pubDate>
		<dc:creator>Tobias Cohen</dc:creator>
				<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://tobiascohen.com/?p=116</guid>
		<description><![CDATA[Having recently installed Cygwin on my windows machine in order to get access to helpful Unix stuff like Git, I soon found the need to set some custom defaults, such as the PATH variable. The standard way to do this is to create a file called .bashrc in your home directory, containing something like this:

PATH=$PATH:/cygdrive/c/mysql4.1.36/bin
export [...]]]></description>
			<content:encoded><![CDATA[<p>Having recently installed Cygwin on my windows machine in order to get access to helpful Unix stuff like Git, I soon found the need to set some custom defaults, such as the PATH variable. The standard way to do this is to create a file called .bashrc in your home directory, containing something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">PATH</span>=<span style="color: #007800;">$PATH</span>:<span style="color: #000000; font-weight: bold;">/</span>cygdrive<span style="color: #000000; font-weight: bold;">/</span>c<span style="color: #000000; font-weight: bold;">/</span>mysql4.1.36<span style="color: #000000; font-weight: bold;">/</span>bin
<span style="color: #7a0874; font-weight: bold;">export</span> PATH</pre></div></div>

<p>Creating this file, and then launching the bash command worked fine for me, but when it came to launching Cygwin directly, it would ignore my .bashrc and start from a clean slate every time.</p>
<p>Looking around on the internet I came across two solutions:</p>
<ol>
<li>
Type into the Cygwin shell:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-p</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>defaults<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>skel<span style="color: #000000; font-weight: bold;">/</span>.bash_profile ~</pre></div></div>

</li>
<li>
Add to the end of /etc/profile:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">. <span style="color: #ff0000;">&quot;<span style="color: #007800;">$HOME</span>/.bashrc&quot;</span></pre></div></div>

</li>
</ol>
<p>I did both of these and it fixed my problem, but you may only need one.</p>
]]></content:encoded>
			<wfw:commentRss>http://tobiascohen.com/2009/08/26/making-cygwin-look-at-your-bashrc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>References are not Pointers</title>
		<link>http://tobiascohen.com/2009/04/14/references-are-not-pointers/</link>
		<comments>http://tobiascohen.com/2009/04/14/references-are-not-pointers/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 11:13:21 +0000</pubDate>
		<dc:creator>Tobias Cohen</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=7</guid>
		<description><![CDATA[I have to admit, I've been working quite happily in PHP for years without ever really needing to use a pointer. Nevertheless, eventually you'll come across a problem that just calls out for a pointer, as I did the other day. This is when I discovered that PHP does not actually have pointers.]]></description>
			<content:encoded><![CDATA[<p>I have to admit, I&#8217;ve been working quite happily in PHP for years without ever really needing to use a pointer. Nevertheless, eventually you&#8217;ll come across a problem that just calls out for a pointer, as I did the other day. This is when I discovered that PHP does not actually have pointers.</p>
<p>If you&#8217;ve ever seen an &#8220;&amp;&#8221; symbol in PHP code, you could certainly be excused for confusing it with a pointer. Here&#8217;s an example of &#8220;&amp;&#8221;:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//&gt; Hello</span></pre></td></tr></table></div>

<p>It sure looks like $b contains a pointer to $a, right? In reality, the &#8220;&amp;&#8221; symbol invokes a peculiar PHP construct called a reference, which unfortunately works an awful lot like a pointer without actually being one. In addition, &#8220;&amp;&#8221; isn&#8217;t really an operator, at least not in this context. PHP is just letting us be lazy and mess up what should look like this: &#8220;$b =&amp; $a&#8221;. The operator is &#8220;=&amp;&#8221;.</p>
<p>Semantics aside, let&#8217;s look at an example where treating a reference like a pointer can mean big trouble:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=&amp;</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$b</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello, World!!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$b</span>...<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//&gt; Hello</span>
<span style="color: #666666; font-style: italic;">//&gt; 5...</span>
<span style="color: #666666; font-style: italic;">//&gt; 4...</span>
<span style="color: #666666; font-style: italic;">//&gt; 3...</span>
<span style="color: #666666; font-style: italic;">//&gt; 2...</span>
<span style="color: #666666; font-style: italic;">//&gt; 1...</span>
<span style="color: #666666; font-style: italic;">//&gt; 0</span></pre></td></tr></table></div>

<p>What&#8217;s going on here? Most of the code worked OK, but when we went to print out the last line, &#8220;Hello World!!&#8221; had been replaced with the number 0. If you look back at the code, you&#8217;ll see that the for loop ends up setting $b to 0, and somehow our &#8220;pointer&#8221; is working backwards, putting the value we assigned to $b back into $a. This is because references do not work like pointers.</p>
<p>So how <em>do</em> references work, then? The <a href="http://au.php.net/manual/en/language.references.whatare.php">PHP manual describes references</a> as being like a hard-link in a unix filesystem. This is a very accurate description, but just in case your unix filesystem knowledge is a little rusty, let me pose an alternate explanation.</p>
<p>Imagine that everything in PHP that starts with a $ sign is actually a pointer, that whenever used is magically dereferenced to take you to the memory where it is stored. I have no idea if this is actually what happens, but it makes a good model. PHP gives you no way to manipulate these pointers, other than using the &#8220;=&amp;&#8221; operator (or <a href="http://au.php.net/manual/en/function.unset.php">unset</a>, more on that later). Doing &#8220;$a =&amp; $b;&#8221; causes the pointer value for $a to be replaced with the pointer value for $b. From now on, and $a and $b both point to <em>the same bit of memory</em>. This explains what happened in the previous example. $a and $b both point to the same thing, so when we used $b in our for loop, it overwrote the value in $a.</p>
<p>If we want to change <em>where it is that $a points</em>, instead of changing the <em>value that is stored there</em>, we have only two options: use another &#8220;=&amp;&#8221; to point $a somewhere else, or use the <a href="http://au.php.net/manual/en/function.unset.php">unset</a> function to get rid of the pointer for $a altogether, leaving our symbol &#8220;$a&#8221; free to be used for a brand new value in a shiny new memory location.</p>
<p>Now we&#8217;ve clarified how references work, I want to warn you that they can potentially be very <em>dangerous</em>, especially when used in the global scope. Consider the second paragraph from our last example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello, World!!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$b</span>...<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If just look at this code alone, there&#8217;s no reason it shouldn&#8217;t work. The reference we created in the first part of this example <em>destroyed the sanity of our coding environment</em>. Once you&#8217;ve created a reference, there&#8217;s really no practical way to look at some other piece of code in the same scope and know that it will work. Thankfully we can avoid much of these problems by using functions or methods to keep our scope separate, however if you do end up setting a reference on a global variable, it&#8217;s very important that you <em>clear it up afterwards using the <a href="http://au.php.net/manual/en/function.unset.php">unset</a> command</em>.</p>
<p>Here&#8217;s our example with the <a href="http://au.php.net/manual/en/function.unset.php">unset</a> function added in to clean up our reference:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=&amp;</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$b</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//&lt;-- added</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello, World!!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$b</span>...<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//&gt; Hello</span>
<span style="color: #666666; font-style: italic;">//&gt; 5...</span>
<span style="color: #666666; font-style: italic;">//&gt; 4...</span>
<span style="color: #666666; font-style: italic;">//&gt; 3...</span>
<span style="color: #666666; font-style: italic;">//&gt; 2...</span>
<span style="color: #666666; font-style: italic;">//&gt; 1...</span>
<span style="color: #666666; font-style: italic;">//&gt; Hello World!!</span></pre></td></tr></table></div>

<p>And our function now works as intended.</p>
]]></content:encoded>
			<wfw:commentRss>http://tobiascohen.com/2009/04/14/references-are-not-pointers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
