<?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>PHP Reference Book Blog &#187; ampersand</title>
	<atom:link href="http://www.phpreferencebook.com/tag/ampersand/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpreferencebook.com</link>
	<description>PHP Reference: Beginner to Intermediate PHP5</description>
	<lastBuildDate>Thu, 29 Sep 2011 03:50:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>PHP &amp; Ampersand: Passing by Reference</title>
		<link>http://www.phpreferencebook.com/samples/php-pass-by-reference/</link>
		<comments>http://www.phpreferencebook.com/samples/php-pass-by-reference/#comments</comments>
		<pubDate>Sun, 01 Mar 2009 00:04:25 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Samples]]></category>
		<category><![CDATA[ampersand]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=165</guid>
		<description><![CDATA[The following PHP Reference excerpt is from pages 20-21. &#38; &#8211; Pass by Reference References allow two variables to refer to the same content. In other words, a variable points to its content (rather than becoming that content). Passing by reference allows two variables to point to the same content under different names. The ampersand [...]]]></description>
			<content:encoded><![CDATA[<p>The following <a href="http://www.phpreferencebook.com/">PHP Reference</a> excerpt is from pages 20-21.</p>
<p style="background: #000000 none repeat scroll 0% 0%; margin-bottom: 0.07in;" lang="en-US"><span style="color: #ffffff;"><span style="font-family: Palatino Linotype,serif;">&amp; &#8211; Pass by Reference</span></span></p>
<p style="margin-bottom: 0.07in;" lang="en-US"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;">References allow two variables to refer to the same content. In other words, a variable points to its content (rather than becoming that content). Passing by reference allows two variables to point to the same content under different names. The ampersand ( &amp; ) is placed before the variable to be referenced.</span></span></p>
<p style="margin-bottom: 0.07in;" lang="en-US"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;"><em>Examples:</em></span></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</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: #666666; font-style: italic;">// $b references the same value as $a, currently 1</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 1 is added to $b, which effects $a the same way</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;b is equal to <span style="color: #006699; font-weight: bold;">$b</span>, and a is equal to <span style="color: #006699; font-weight: bold;">$a</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<pre style="background: #c0c0c0 none repeat scroll 0% 0%; margin-bottom: 0.07in; font-style: normal;" lang="en-US"><span style="color: #000000;"><span style="font-family: Courier New,monospace;">b is equal to 2, and a is equal to 2</span></span></pre>
<p style="margin-bottom: 0.07in;" align="center"><span style="font-family: Symbol;"></span></p>
<p style="margin-bottom: 0.07in;" lang="en-US"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;">Use this for functions when you wish to simply alter the original variable and return it again to the same variable name with its new value assigned.</span></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> add<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// The &amp;amp; is before the argument $var</span>
<span style="color: #000088;">$var</span><span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
add<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;a is <span style="color: #006699; font-weight: bold;">$a</span>,&quot;</span><span style="color: #339933;">;</span>
add<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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot; a is <span style="color: #006699; font-weight: bold;">$a</span>, and b is <span style="color: #006699; font-weight: bold;">$b</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Note: $a and $b are NOT referenced</span></pre></div></div>

<pre style="background: #c0c0c0 none repeat scroll 0% 0%; margin-bottom: 0.07in;">a is 2, a is 2, and b is 11</pre>
<p style="margin-bottom: 0.07in;" lang="en-US"><span style="color: #000000;"><span style="font-family: Palatino Linotype,serif;">You can also do this to alter an array with foreach:</span></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">unset</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Must be included, $value remains after foreach loop</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<pre style="background: #c0c0c0 none repeat scroll 0% 0%; margin-bottom: 0.07in;">Array ( [0] =&gt; 11 [1] =&gt; 12 [2] =&gt; 13 [3] =&gt; 14 )</pre>
<p>
<hr width='50%' />
<p>What tricks do you have for using the ampersand in PHP to pass by reference?<br />
Leave them in the comments below!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/samples/php-pass-by-reference/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

