<?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>The Sapience Society &#187; array</title> <atom:link href="http://blog.lococobra.com/tag/array/feed" rel="self" type="application/rss+xml" /><link>http://blog.lococobra.com</link> <description>The advice of a self-proclaimed technological ninjician</description> <lastBuildDate>Thu, 08 Dec 2011 19:41:17 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>PHP Variable Variables and Reference Arrays in Use</title><link>http://blog.lococobra.com/php-variable-variables-reference-arrays</link> <comments>http://blog.lococobra.com/php-variable-variables-reference-arrays#comments</comments> <pubDate>Wed, 23 Sep 2009 18:37:27 +0000</pubDate> <dc:creator>Jeff</dc:creator> <category><![CDATA[Code Magic]]></category> <category><![CDATA[array]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[reference]]></category> <guid
isPermaLink="false">http://blog.lococobra.com/?p=220</guid> <description><![CDATA[Something I haven't talked about a whole lot on here is that I'm actually a professional PHP developer. It's my job, so usually I don't mix work and online stuff. This is pretty cool though, so I want people to be able to find it if they're trying to figure out how to do it. [...]]]></description> <content:encoded><![CDATA[<p>Something I haven't talked about a whole lot on here is that I'm actually a professional PHP developer. It's my job, so usually I don't mix work and online stuff. This is pretty cool though, so I want people to be able to find it if they're trying to figure out how to do it. Be warned, if you're not a PHP coder the following post will make ZERO sense to you <img
src='http://blog.lococobra.com/wp-includes/images/smilies/icon_razz.gif' alt="icon razz PHP Variable Variables and Reference Arrays in Use" class='wp-smiley' title="PHP Variable Variables and Reference Arrays in Use" /></p><p>Before I get into what a "reference array" is (I made that term up) let me guide you through the discovery and some practical uses of the theory around references. A while ago I was looking for a way to do something in code that didn't seem very possible. I wanted to do this:</p><pre class="brush: php; title: ; notranslate">$var1 = 'Hello World';
echo $&quot;var1&quot;;</pre><p>Problem is, that doesn't work.. instead I found that you can use variable variables (also known as dynamic variables)<br
/> <span
id="more-220"></span></p><h3>Variable Variables</h3><pre class="brush: php; title: ; notranslate">$var1 = 'Hello World';
$var2 = 'var1';
echo $$var2;
Output: Hello World</pre><p>So how does that code work? PHP parses the inside string and evaluates which outside string to call.</p><pre class="brush: php; title: ; notranslate">IF: $var1 = 'Hello World';
$var2 = 'var1';
THEN: $var1 == $$var2</pre><p>But what's the practical use? Why on earth would we call a variable that way? When could there possibly be a situation where you don't already know the name of the variables you're accessing? I'll tell you with an example, it's one of my favorite pieces of code I've ever written. It's short and elegant and would probably be used by 80% of developers if they knew about it.</p><pre class="brush: php; title: ; notranslate">
foreach($_REQUEST as $key=&gt;$elem)
	$$key = $elem;</pre><p>That code takes every $_GET, $_POST, and $_COOKIE variable and converts it like this:</p><pre class="brush: php; title: ; notranslate">IF: $_POST['foo'] = 'bar';
THEN: $foo = 'bar';</pre><p>Form processing just became so much easier! Plus if you want you can add trim and stripslashes to make form processing even easier...</p><pre class="brush: php; title: ; notranslate">foreach($_REQUEST as $key=&gt;$elem)
	$$key = trim(stripslashes($elem));</pre><p>This is all well and good, but there's still a completely different way to do all this.</p><h3>Using References</h3><pre class="brush: php; title: ; notranslate">$var1 = 'Hello World';
$var2 = &amp;$var1;
echo $var2;
OUTPUT: Hello World</pre><p>In that code, $var2 is created as a reference to $var1, meaning the two can be used interchangeably. You can reassign the value of either $var1 or $var2 and both variables will reflect the change. What's interesting is, references can not be used to do the code I wrote earlier! Anyways, one more example before we get to reference arrays.</p><pre class="brush: php; title: ; notranslate">$var1 = 'Hello';
$var2 = ', ';
$var3 = 'World';
$varArr = array(&amp;$var1, &amp;$var2, &amp;$var3);
$var1 = 'Goodbye';
$var2 = ' Cruel ';
foreach($varArr as $item)
	echo $item;
OUTPUT: Goodbye Cruel World</pre><p>What's important here? You can use references to create an array which will always reflect the current information being stored in other variables. This can still be replicated by the double string approach though. Example:</p><pre class="brush: php; title: ; notranslate">$var1 = 'Hello';
$var2 = ', ';
$var3 = 'World';
$varArr = array('var1', 'var2', 'var3');
$var1 = 'Goodbye';
$var2 = ' Cruel ';
foreach($varArr as $item)
	echo $$item;
OUTPUT: Goodbye Cruel World</pre><p> So by now if you're still even reading you're probably thinking "Why even use this stuff? What's the point? Especially what's the point of reference arrays?"</p><h3>Using Reference Arrays</h3><p>Lets say have several arrays filled with similar content, and we need to do some processing to the data in those arrays. You could combine the arrays, but what if you need them to be separate? I'll leave you with this rather simple example. If you have questions please post comments and I'll answer them.</p><pre class="brush: php; title: ; notranslate">
$fruits = array(' appLE', 'pear3', 'banana--');
$vegetables = array('pea', 'broccoli   ');
$processArr = array(&amp;$fruits, &amp;$vegetables);
foreach($processArr as &amp;$array)
	foreach($array as &amp;$item)
	{
		$item = preg_replace('/[^a-z]/i', '', $item);
		$item = ucwords(strtolower($item));
	}
echo '&amp;lt;pre&gt;';
print_r($fruits);
print_r($vegetables);
</pre><div
class="shr-publisher-220"></div>]]></content:encoded> <wfw:commentRss>http://blog.lococobra.com/php-variable-variables-reference-arrays/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
