<?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>Albert Fama &#187; tips</title>
	<atom:link href="http://albertfama.com/tag/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://albertfama.com</link>
	<description>Freelance Web Programmer - specializing in PHP &#38; MySQL</description>
	<lastBuildDate>Fri, 20 Nov 2009 16:06:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Learning PHP</title>
		<link>http://albertfama.com/php/learning-php/</link>
		<comments>http://albertfama.com/php/learning-php/#comments</comments>
		<pubDate>Wed, 21 Nov 2007 19:56:12 +0000</pubDate>
		<dc:creator>Albert Fama</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://obnexus.net/?p=40</guid>
		<description><![CDATA[Over the weekend I was visiting a friend, who has his own website. He uses PHP on the site, but he is not a programmer. The site is his hobby and he has only learned enough PHP to get the job done. Simply put this is not his occupation. While visiting he was in the [...]]]></description>
			<content:encoded><![CDATA[<p>Over the weekend I was visiting a friend, who has his own website. He uses PHP on the site, but he is not a programmer. The site is his hobby and he has only learned enough PHP to get the job done. Simply put this is not his occupation. </p>
<p>While visiting he was in the middle of writing a small script which queried articles out of a database and displayed them according to the categories they were assigned to. Everything was working correctly, but the processing time was slower than he would like. He asked if I could take a look and see if anything could be done to speed things up. While optimizing some of the PHP and SQL code; I was showing him different functions which are built into PHP and MySQL, (these functions will always execute faster than the ones you write yourself). </p>
<p>As I was standing over his shoulder pointing out the different problematic areas, he asked me how I was able to keep all that information in my head. I simply replied that it is my job, it&#8217;s what I get paid to do. After going back and forth, what he actually wanted to know was how I  memorized all the different functions, which parameters they accept, and in what order. This is not the first time I have been asked this question so I decided to actually think about how it all happened.</p>
<p>I once read the book &#8216;On Writing&#8217; (by Stephen King), it is a book about how to become a writer. I don&#8217;t really remember a lot about the book, but I do remember one thing. He said that if you want to be a writer, you need to write everyday. It does not have to be anything great, just write something. I attempted to do this, but it became a chore and I soon stopped writing.</p>
<p>After thinking about the question my friend asked (I admit I have a horrible memory) but just like King said, do it everyday. I learned PHP simply by writing code everyday, by using PHP constantly, and little by little things sink in and stay. I never sat down with the manual and made an effort to memorize things, it just naturally happened over a period of time.</p>
<p>When I first started, I remember the fascination I had with writing something that made a computer do what I wanted. It was almost like an addiction, once I got it to do one thing, I wanted write something more complicated. Granted over the years it takes more, but I am now writing more complicated code and when it works, I get that same feeling.</p>
<p>So if you want to become a coder that is able to write scripts off the top of your head, or be able to &#8216;see&#8217; the code before you even begin, do it everyday. It does not have to be ground breaking or a completely new idea, just write something. If you find that writing code everyday becomes a chore, then you may want to try your hand at something else, maybe become an author.</p>

                            <div id="aspdf">
                                <a href="http://albertfama.com/wp-content/plugins/as-pdf/generate.php?post=40">
                                    <span>&nbsp;</span>
                                </a>
                            </div>
                        ]]></content:encoded>
			<wfw:commentRss>http://albertfama.com/php/learning-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Variable Variables</title>
		<link>http://albertfama.com/php/variable-variables/</link>
		<comments>http://albertfama.com/php/variable-variables/#comments</comments>
		<pubDate>Mon, 19 Nov 2007 21:11:29 +0000</pubDate>
		<dc:creator>Albert Fama</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Variables]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[variable variables]]></category>

		<guid isPermaLink="false">http://obnexus.net/?p=36</guid>
		<description><![CDATA[It is a rare occurrence that I find an excuse to use variable variables, but when that situation arises they are not only helpful but also fun in a way. I have often heard beginning programmers say that they find the concept difficult to grasp. I believe the confusion doesn&#8217;t stem from understanding the concept [...]]]></description>
			<content:encoded><![CDATA[<p>It is a rare occurrence that I find an excuse to use variable variables, but when that situation arises they are not only helpful but also fun in a way. I have often heard beginning programmers say that they find the concept difficult to grasp. I believe the confusion doesn&#8217;t stem from understanding the concept itself; but from understanding when a variable variable would be beneficial. In this article I will attempt to dispel the myth by including real-world examples along with the basic information about variable variables.</p>
<p>One thing that should be mentioned before we begin, variable variables should be used sparingly. Their overuse can make scripts difficult to debug and confusing six months down the road when you decide to update or add functionality to your code. So before you use them make sure the problem your trying to solve warrants their use. (Hopefully the examples used in this article will give you the background to make that decision.)</p>
<p>We will begin by explaining what variable variables are. First let see what the PHP manual says a variable variable is:</p>
<blockquote><p><strong>PHP Manual:</strong><br />
A <a href="http://www.php.net/manual/en/language.variables.variable.php"  title="PHP Manual: Variable Variables">variable variable</a> takes the value of a variable and treats that as the name of a variable.</p></blockquote>
<p>Let&#8217;s see if we can add a little more to it&#8230; A variable variable, is created when two dollar signs ($$) are placed at the beginning of a variable name. The PHP engine interprets this to mean the value of the variable (which has two dollar signs in front of it) is the name of the variable which needs to be interpreted.</p>
<p>Not sure if that is any clearer, so lets go to some code so we can see it in action, then I&#8217;m sure everyone will understand. </p>
<pre name="code" class="php">
&lt;?php
$site = &quot;NULL&quot;;
$ring = &quot;NULL&quot;;
$plug = &quot;NULL&quot;;
&nbsp;
$entity_1 = &quot;site&quot;;
$entity_2 = &quot;plug&quot;;
$id_1     = 54;
$id_2     = 78;
&nbsp;
$$entity_1 = $id_1;
$$entity_2 = $id_2;
&nbsp;
echo &quot;site: &quot;.$site.&quot;&lt;br /&gt;&quot;;
echo &quot;ring: &quot;.$ring.&quot;&lt;br /&gt;&quot;;
echo &quot;plug: &quot;.$plug.&quot;&lt;br /&gt;&quot;;
?>
</pre>
<p>When this code is run it will print:<br />
<code>site: 54<br />
ring: NULL<br />
plug: 78<br />
</code><br />
Notice the two dollar signs ($$) in front of the second occurrence of the variables &#8216;entity_1&#8242; and &#8216;entity_2&#8242;, these are the variable variables. Since the value of the variable $entity_1 is &#8216;site&#8217; when PHP parses:</p>
<pre name="code" class="php">
$$entity_1 = $id_1;
</pre>
<p>it <em>reads</em>:<br />
<code>$site = $id_1;</code><br />
The same also happens for $$entity_2. </p>
<p>That&#8217;s it! That is what a variable variable is, nothing really ground breaking here. So the question is when should this <em>feature</em> of PHP be used?</p>
<p>First I have to admit that I have never run into a situation where variable variables are required to get the job done. I think it is very telling when the section on variable variables in the PHP Manual starts with the sentence:</p>
<blockquote><p>
Sometimes it is convenient to be able to have variable variable names.
</p></blockquote>
<p>The keyword here being: convenient (as in &#8216;not necessary&#8217;).</p>
<p>The most recent situation where I used variable variables was this:<br />
I was writing a function which managed the data in a database correlation table. The table had three columns: &#8216;site_id&#8217;, &#8216;ring_id&#8217;, and &#8216;plug_id&#8217;. </p>
<p>The function I was working on accepted the entity types and ids for two entities. Hence, the function declaration looked like this:
<pre name="code" class="php">
function&nbsp;correlate($entity_1,&nbsp;$id_1,&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$entity_2,&nbsp;$id_2)
</pre>
<p>That function used another function which was responsible for actually inserting a record into the table and its declaration looked like this:</p>
<pre name="code" class="php">
function create($site_id,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ring_id,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$plug_id)
</pre>
<p>The purpose of the function I was writing was to determine if a record existed for either entity in the db table, if so update the correlation, if not insert a new record. Variable variables came into play if a record needed to be inserted into the correlation table. </p>
<p>Once it was determined that a new record need to be inserted, I created three variables set to the column defaults (NULL): </p>
<pre name="code" class="php">
function&nbsp;correlate($entity_1,&nbsp;$id_1,&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$entity_2,&nbsp;$id_2)
{
&nbsp;&nbsp;&nbsp;&nbsp;//check for record
&nbsp;&nbsp;&nbsp;&nbsp;if ($record) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//update and return
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$site = &quot;NULL&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;$ring = &quot;NULL&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;$plug = &quot;NULL&quot;;
}
</pre>
<p>Without some checking the function did not &#8216;know&#8217; which ids it had. So to make things easier I decided to use variable variables and write the rest of the function like this:</p>
<pre name="code" class="php">
function&nbsp;correlate($entity_1,&nbsp;$id_1,&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$entity_2,&nbsp;$id_2)
{
&nbsp;&nbsp;&nbsp;&nbsp;//check for record
&nbsp;&nbsp;&nbsp;&nbsp;if ($record) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//update and return
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$site = &quot;NULL&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;$ring = &quot;NULL&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;$plug = &quot;NULL&quot;;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$$entity_1 = $id_1;
&nbsp;&nbsp;&nbsp;&nbsp;$$entity_2 = $id_2;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;return create($site, $ride, $plug);
}
</pre>
<p>Again variable variables were not needed in this situation I could have used &#8216;switch&#8217; or &#8216;if&#8217; statements to determine which entity types were sent, but I thought the use of variable variables was a cleaner option and quite frankly was faster to code.</p>
<p>BTW &#8211; If anyone has run into a situation where variable variables were required, please let me know I would love to hear about it.</p>

                            <div id="aspdf">
                                <a href="http://albertfama.com/wp-content/plugins/as-pdf/generate.php?post=36">
                                    <span>&nbsp;</span>
                                </a>
                            </div>
                        ]]></content:encoded>
			<wfw:commentRss>http://albertfama.com/php/variable-variables/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

