<?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 PHP Coder&#039;s Judy Chop</title>
	<atom:link href="http://phpadvocate.com/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://phpadvocate.com/blog</link>
	<description></description>
	<lastBuildDate>Tue, 25 May 2010 20:09:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Javascript &#8211; Is Length a Property or Method?</title>
		<link>http://phpadvocate.com/blog/?p=153</link>
		<comments>http://phpadvocate.com/blog/?p=153#comments</comments>
		<pubDate>Tue, 25 May 2010 16:29:14 +0000</pubDate>
		<dc:creator>Jonathon Hibbard</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Array Object]]></category>
		<category><![CDATA[Length]]></category>
		<category><![CDATA[SpiderMonkey]]></category>

		<guid isPermaLink="false">http://phpadvocate.com/blog/?p=153</guid>
		<description><![CDATA[Today, a buddy of mine (Jeremy Daley) asked me a (seemingly) simple question: &#8220;Did we ever establish if it was more beneficial to grab the length of an array before a for loop?  instead of including &#60;array&#62;.length within the &#8220;for&#8221; params?&#8221; Obviously if the question were related to PHP the answer is quite evident: get [...]]]></description>
			<content:encoded><![CDATA[<p>Today, a buddy of mine (<a title="The Daley Jem" href="http://www.daleyjem.com" target="_blank">Jeremy Daley</a>) asked me a (seemingly) simple question: &#8220;Did we ever establish if it was more beneficial to grab the length of an array before a for loop?  instead of including &lt;array&gt;.length within the &#8220;for&#8221; params?&#8221;</p>
<p>Obviously if the question <a href="http://phpadvocate.com/blog/?p=35">were related to PHP</a> the answer is quite evident: get the length first, then loop on the result.  For Javascript, this is a bit more confusing of a subject.  In many articles, you&#8217;ll notice that the Javascript community in whole is split on if Length is, indeed, a Property or a Method.  This is very much true when associating this against the Array object.</p>
<p>Doing a simple query in Google for &#8220;Javascript is length a method or object&#8221; won&#8217;t really get you the results you want (unless you search now of course <img src='http://phpadvocate.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ).  But instead you will see many articles that throw around &#8220;property&#8221; or &#8220;method&#8221; being what Length is, thus making the concept that much more difficult to understand.</p>
<p>After digging around, I finally came to the actual <a href="http://mxr.mozilla.org/mozilla/source/js/src/" target="_blank">source for Mozilla&#8217;s SpiderMonkey</a> (the Javascript Engine for FireFox) to find the answer.  First things first, what is the source code of <a href="http://mxr.mozilla.org/mozilla/source/js/src/jsarray.c" target="_blank">Array</a>?  That was the most intriguing question.  The second was, is Length defined as a <strong>method</strong> or does it hold a statically defined <strong>property</strong> for our array object?</p>
<p>The answer really surprised me.  I actually thought for sure that &#8220;length&#8221; would be its separate method entirely, but instead found that it was, in fact, a property inside the Array class itself.  <strong>But</strong>, wait a minute, that&#8217;s not <em>entirely</em> true.  While it <em>is</em> a <strong>property</strong>, it instead is a <a href="http://ejohn.org/blog/javascript-getters-and-setters/" target="_blank">setter</a> <strong>method</strong> property.  In other words, the &#8220;property&#8221; of the array object is never really determined until you actually <em>call</em> the &#8220;length&#8221; setter property itself.  So, while you&#8217;re able to say &#8220;my_array.length&#8221; to obtain the length of your array, it actually is calling the setter <strong>method</strong> that calculates what exactly your array length is.</p>
<p>There is no wonders this concept is so easily misconstrued as saying that &#8220;length&#8221; is a property.  Because on one side of the coin its exactly that: a property of an object.  But until we actually look at the source code itself to see how array is coded, we see immediately that it is, actually, just a normal calculation <strong>method</strong>.</p>
<p>What does this mean?  It means that our assumptions to begin with (which method is faster) were true.  If we get the length of an array and define that as a variable, and then loop and calculate on that var, we can in turn create a faster more optimized <strong>for loop</strong>.  If instead we decide to call &#8220;length&#8221; on every iteration, we&#8217;re actually creating more overhead!</p>
<p><strong>Note: </strong>It is also good to note here that if we try to issue typeof() on say myArray.length (ie: alert(typeof(myArray.length));) you&#8217;ll just get an integer as a result.  This shouldn&#8217;t be too confusing as it&#8217;s doing a typeof on the return result here, which is, in fact, an integer.</p>
<p>A simple test case that Jeremy gave is the following code:</p>
<pre>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:800px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> myArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.php.net/array"><span style="color: #990000;">Array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1000000</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; myArray<span style="color: #339933;">.</span>push<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dude&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">var</span> startTime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.php.net/date"><span style="color: #990000;">Date</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>getTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">//for (var i = 0; i &lt; myArray.length; i++)</span><br />
<br />
<span style="color: #000000; font-weight: bold;">var</span> arrayCount <span style="color: #339933;">=</span> myArray<span style="color: #339933;">.</span>length<span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> arrayCount<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">var</span> endTime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.php.net/date"><span style="color: #990000;">Date</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>getTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
alert<span style="color: #009900;">&#40;</span>endTime <span style="color: #339933;">-</span> startTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
</pre>
<p>If we run this test (locally of course) we&#8217;ll see a 2millisecond average run-time.  If we comment out our length var and for loop, and instead uncomment the for loop that&#8217;s currently commented out, we&#8217;ll notice that we get an average load-time of 6milliseconds.</p>
<pre>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:800px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> myArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.php.net/array"><span style="color: #990000;">Array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1000000</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; myArray<span style="color: #339933;">.</span>push<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dude&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">var</span> startTime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.php.net/date"><span style="color: #990000;">Date</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>getTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> myArray<span style="color: #339933;">.</span>length<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #666666; font-style: italic;">//var arrayCount = myArray.length;</span><br />
<span style="color: #666666; font-style: italic;">//for (var i = 0; i &lt; arrayCount; i++) { }</span><br />
<br />
<span style="color: #000000; font-weight: bold;">var</span> endTime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.php.net/date"><span style="color: #990000;">Date</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>getTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
alert<span style="color: #009900;">&#40;</span>endTime <span style="color: #339933;">-</span> startTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
</pre>
<p>It should also be noted here, that I did not check other Javascript Engines for other browsers.  I realize that this could be a bit of a discussion amongst the groups, but I&#8217;m quite satisfied relying on what Mozilla&#8217;s team is doing (I really, honestly, could care less what IE is doing).   So, maybe one day I&#8217;ll do this digging (or someone else could, either way..) but for now, I&#8217;m happy with what we have at the moment.</p>
<p>One may also ask the question &#8220;What does this even matter if length for an Array Object is a method or a property?&#8221;.  The answer should come to you almost immediately.  If length were, indeed, a property that always has the most updated information, then we could in turn treat the &#8220;property&#8221; as we would any variable.  This would be beneficial since we then could include the length property question (as we did in the second test example script above) and notice absolutely no difference in overhead versus the first example script.  But, since this is not a true variable, and instead is a method, that means every iteration of our for loop, we are constantly calling the length method to crunch the array and return the count of indexes it finds.  This in turn creates the overhead that we so desperately want to avoid when considering an optimized frontend when considering loadtime and speed.</p>
<p>Conclusion?  Both worlds are correct about &#8220;property&#8221; or &#8220;method&#8221;, but the more definitive answer is that it&#8217;s actually, at the end of the day, just a method.  Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://phpadvocate.com/blog/?feed=rss2&amp;p=153</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Art of Agile Development</title>
		<link>http://phpadvocate.com/blog/?p=150</link>
		<comments>http://phpadvocate.com/blog/?p=150#comments</comments>
		<pubDate>Tue, 06 Apr 2010 17:09:23 +0000</pubDate>
		<dc:creator>Jonathon Hibbard</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Agile Development]]></category>

		<guid isPermaLink="false">http://phpadvocate.com/blog/?p=150</guid>
		<description><![CDATA[Today, I bought a book called &#8220;The Art of Agile Development&#8221; by James Shore &#38; Shane Warden.  To me, it&#8217;s kind of like the developer&#8217;s equivalent to the Holy Grail.  I&#8217;ll be reading this over the next few days (hell, maybe weeks if it takes that long) and will write a formal review of it.  [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I bought a book called &#8220;The Art of Agile Development&#8221; by James Shore &amp; Shane Warden.  To me, it&#8217;s kind of like the developer&#8217;s equivalent to the Holy Grail.  I&#8217;ll be reading this over the next few days (hell, maybe weeks if it takes that long) and will write a formal review of it.  But as of right now, it&#8217;s bringing pure chills of excitement to me again after a long time.  Stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://phpadvocate.com/blog/?feed=rss2&amp;p=150</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Facebook API 101 &#8211; OINK-PUG Presentation</title>
		<link>http://phpadvocate.com/blog/?p=142</link>
		<comments>http://phpadvocate.com/blog/?p=142#comments</comments>
		<pubDate>Fri, 19 Mar 2010 03:10:55 +0000</pubDate>
		<dc:creator>Jonathon Hibbard</dc:creator>
				<category><![CDATA[Facebook API]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://phpadvocate.com/blog/?p=142</guid>
		<description><![CDATA[&#8220;Facebook API 101&#8243; by presenter Jonathon Hibbard In this presentation, I hope to provide you with as much information as possible to start building your own very basic applications based on Facebook&#8217;s great API model. The API model, while unknown to many developers, is quite simple. However, getting started with it can be very challenging [...]]]></description>
			<content:encoded><![CDATA[<h3>&#8220;Facebook API 101&#8243; by presenter Jonathon Hibbard</h3>
<p>In this presentation, I hope to provide you with as much information as possible to start building your own very basic applications based on Facebook&#8217;s great API model. The API model, while unknown to many developers, is quite simple. However, getting started with it can be very challenging (as with any API, or new concept that one may (or may not) be unfamiliar with).</p>
<p>Since the Facebook API has close to a hundred PHP functions, and easily over a thousand FBML (Facebook Markup Language) tags and settings, we will instead just focus on the very basics to get you on your way.</p>
<p>The following topics will be covered in this presentation:</p>
<ul>
<li>Steps for getting your first Facebook Application registered and ready for the Facebook Community.</li>
<li>Setting up your directory that will host your facebook application.</li>
<li>Getting and unpacking the Facebook API class files.</li>
<li>Configuring the Facebook Application settings.</li>
<li>Understanding the basics of the FBML and the actual PHP Classes associated with this API.</li>
<li>How to setup a basic DB-driven application to be used with Facebook&#8217;s API (examples included)</li>
<li>Helpful sites, and starter tutorials that can also help</li>
</ul>
<p>From Facebook&#8217;s Developer page, they suggest you know or need the following:</p>
<ul>
<li>You should be well versed in PHP or some other coding language &#8212; such as Ruby on Rails, JavaScript, or Python &#8212; especially one that has a client library for our API.</li>
<li>You need to have a basic understanding of the Internet, SSH, MySQL, and Unix</li>
<li>You need to be familiar with Web hosting fundamentals and have a place to host your application.</li>
</ul>
<p>I would add that you should also be familiar with XML as the syntax for FBML is similar to a small degree, and it will only make learning this markup language easier.</p>
<p>So, without further ado, let&#8217;s begin.</p>
<hr />
<h3>I. Steps for getting your first Facebook Application registered and ready for the Facebook Community</h3>
<p>While understanding Facebook&#8217;s API, FBML, FQL, and FJS is very important for building a Facebook application, we must also understand the principals involved with registering our application and obtaining the necessary Keys to interact with their environment (just like we do with Google Maps or any other 3rd party site who has an API we wish to use).</p>
<p>First we need to Login to the Facebook Developer section. To do this, we&#8217;ll go to their developer landing page (http://developers.facebook.com/get_started.php), and click &#8220;Go to the Facebook Developer App&#8221;. If you notice, the login looks the same as if we were trying to log into facebook normally, but they have a redirect in that sends us to the Developer&#8217;s section we will need to be in to register our application.</p>
<p>On this page, we&#8217;ll simply click &#8220;Set Up New Application&#8221;. This is where we finally start defining what our application will be named, hosted, etc.</p>
<p>The first screen we see is the Application Name. This name can be changed at any time, so we will just use something generic, Accept their terms, and go on our way.</p>
<p>What we are directed to next is what you will become quite familiar with over time: the application settings page. This page (as I stated earlier) not only allows you to change the name of your application, but also :</p>
<ul>
<li>Is where you&#8217;ll find the API Key / Secret Key that you&#8217;ll use through Facebook&#8217;s API connection to be granted access.</li>
<li>Where you can define how your application will be presented to your audience (ie: Description, Icon, Logo, etc)</li>
<li>Where you define the location of your application that lives on your own personal hosting platform</li>
</ul>
<p>There are many other settings and things you can do on this page, but the basics I&#8217;ve stated above are what we need as the rest is out of scope of this presentation. We will revisit these settings in a minute.</p>
<p>For now, let&#8217;s leave the settings page and grab the Facebook API and start setting up the environment on our hosting server where our tool is going to live.</p>
<hr />
<h3>II. Setting up your directory that will host your facebook application.</h3>
<p>Setting up your directory to host your Facebook Application is nothing really spectacular. You&#8217;re still going to need to create the directory in the htdocs of your web hosting environment, and you&#8217;re still going to need the same access rules for loading images/css/html pages as you should already be accustomed to.</p>
<p>For our example, we&#8217;ll be creating a directory named oinkpug_101 which will be where our facebook application is going to live.</p>
<p>Inside of our oinkpug_101 directory, we&#8217;ll be creating a small hierarchy of directories.</p>
<p>First, we&#8217;ll create the apps directory that will contain 2 directories: classes and includes.</p>
<pre>cd oinkpug_101
mkdir apps
cd apps
mkdir classes
mkdir includes</pre>
<p>Inside of the classes folder, we&#8217;ll create a directory named facebook that will be where we&#8217;ll host the facebook API.</p>
<pre>cd classes
mkdir facebook</pre>
<p>While there are a number of directory structures one can create, and I&#8217;m sure that&#8217;s a debatable subject among many developers, this is my method i use for creating structure.</p>
<p>Feel free to use whatever method suits you and your needs the best.</p>
<p>And that&#8217;s all there really is to it. Now we&#8217;re ready to get Facebook&#8217;s API package and install it into its new home.</p>
<hr />
<h3>III. Getting and unpacking the Facebook API class files.</h3>
<p>Obtaining the Facebook API can be accomplished many different ways. The two I will suggest are :</p>
<ol>
<li>By clicking &#8220;PHP Client Libraries&#8221; in the Helpful Links section of the original page we were on (http://developers.facebook.com/get_started.php)</li>
<li>By using wget from the console and downloading directly to your server environment (wget http://svn.facebook.com/svnroot/platform/clients/packages/facebook-platform.tar.gz)</li>
</ol>
<p>As with many things in life, there are many other methods and choices, but for me these are the 2 easiest to cover in this introduction.</p>
<p>I&#8217;ll be using the second method for wget to obtain the package, and then extract this tar ball into the directory we created earlier for it (facebook):</p>
<pre>cd oinkpug/apps/classes/facebook
wget http://svn.facebook.com/svnroot/platform/clients/packages/facebook-platform.tar.gz
tar xvfz facebook-platform.tar.gz</pre>
<p>If we do a directory listing we&#8217;ll now see the directory structure this API uses. In it, there are 2 main directories: footprints and php.</p>
<p>The footprints directory is just an example Facebook has given you for understanding how to interact with your own personal Database (which we will cover later).</p>
<p>For now, the only directory we are concerned with is the PHP directory, which houses the actual API classes and methods that we&#8217;ll be using.</p>
<p>The php folder has 1 subfolder (JSON) and 4 PHP class files (facebook.php, facebook_desktop.php, facebook_mobile.php, and facebookapi_php5_restlib.php).</p>
<p>The subdirectory JSON contains the JSON classes for sending/recieving JSON data. This directory will not be covered in this presentation as it is out of scope for a 101 talk. The main purpose of it though is Facebook&#8217;s API sends the vast majority of your method calls to their server by an encapsulated JSON request/response method that will send/receive the data you are requesting/sending.</p>
<p>Instead let&#8217;s look at the 4 php files that are in the root of our php directory. facebook.php is the main class file that&#8217;s used. It&#8217;s sole purpose is to create a connection and handle your initial requests for accessing user/friend information through the FacebookRestClient.</p>
<p>The facebookapi_php5_restlib.php is the FacebookRestClient class that our main Facebook class calls. The FacebookRestClient basically is the nuts and bolts of the actual API to obtain user/group/application information from Facebook&#8217;s server through the JSON class we spoke about earlier.</p>
<p>The other 2 files are facebook_mobile.php and facebook_desktop. As their names imply, the mobile php file is used for developing mobile applications, while the desktop php file is used for creating desktop applications. Just like the JSON class, we will not be covering these two files since they are out of scope for what we are wanting to achieve.</p>
<p>However, there are useful links included in our references point that can guide you in the right direction should you want to explore these alternatives.</p>
<p>Now that we have our files unpacked, and have a very basic understanding of what files we are now hosting (to the world no less), we&#8217;re ready to start setting up our Facebook application settings and also actually creating our first facebook application to be used with Facebook.</p>
<hr />
<h3>IV. Configuring the Facebook Application settings.</h3>
<p>Configuring your facebook application settings is very simple.</p>
<p>We first switch back to Facebook&#8217;s Application Settings page we left earlier.</p>
<p>Under the &#8220;My Applications&#8221; section, we&#8217;ll click on our Application Name (ABC123).</p>
<p>Next we&#8217;ll click Edit Settings and you&#8217;ll notice we&#8217;re right back into the Application Settings page.</p>
<p>Here, our next step should be clicking on the Canvas link located on the left hand side of this page. The first section we&#8217;ll find here is &#8220;Required URLs&#8221; that facebook needs us to define in order to direct request for your applications to not only the facebook application URL (labeled Canvas Page URL on this page), but also the path to where your application is accessed via URL (labeled Canvas Callback URL).</p>
<p>The first url, Canvas Page URL, is going to be something we define that best suits what our application name will be. Our application&#8217;s name is abc123, so in the first text box for http://apps.facebook.com/, we&#8217;ll put abc123.</p>
<p>In the Canvas Callback URL, we&#8217;ll put http://www.phpadvocate.com/OINKPUG_101 .</p>
<p>Since we&#8217;ll be using the Facebook Markup Language (FBML), we&#8217;ll switch the Render Method of FBML.</p>
<p>Finally, we&#8217;ll click save to commit our changes which directs us back to the summary page for our application.</p>
<p>You&#8217;ll notice on this page that Facebook happily gives us our API Key, Application Secret key, and Application ID instead of forcing us back into the application settings page. It also gives us the link we can use while logged into facebook to access our Application which we will use in few minutes to view our first facebook application.</p>
<p>There also exists one more important item, which is labeled Sample Code on this page. If you click on this link, you&#8217;ll notice a div that opens up containing not only a very simple PHP script, but also that they have went ahead and populated your API key and Application Secret key in the code for you. So, we&#8217;ll go ahead and copy this text so that we can create our first facebook page right away with the least amount of trial and error.</p>
<p>We&#8217;ll switch back to our editor that we&#8217;re using for managing our php files and create our first file in the oinkpug_101 directory.</p>
<p>I&#8217;m going to name our first file index.php so that it&#8217;s our landing page, paste the code.</p>
<p>If we look at the code, the first thing we see is the require_once of facebook.php. Our path right away needs to be updated to point to the correct location of facebook.php.</p>
<p>So, we&#8217;ll just add the correct path and save our changes.</p>
<p>So, what does this code actually do? That bring us to our next section.</p>
<hr />
<h3>V. Understanding the basics of the FBML and the actual PHP Classes associated with this API.</h3>
<p>When we examine this file we&#8217;ll see that line 13 shows us how we instantiate the facebook class by passing the api key first, then the application secret second.</p>
<p>The very next line is a foreign to us at the moment and it&#8217;s not quite clear what it does. So, let&#8217;s look at Facebook&#8217;s require_login method definition to see if they have some help for us.</p>
<p>When we read this comment, that tells us absolutely nothing right? So how are we supposed to know that we even need this function? When the code doesn&#8217;t leave us with the proper comments, we resort to Facebook&#8217;s Developer Wiki.</p>
<p>If we go to Client Libraries, then PHP, we&#8217;ll be directed to the Official PHP client Library Page. A simple search for require_login will take us to a section explaining this function as follows:</p>
<pre>require the user to be logged into Facebook before using the application. If they are not logged in they will first be directed to a
Facebook login page and then back to the application's page. 

require_login() returns the user's unique ID which we will store in fb_user</pre>
<p>Obviously their comment says fb_user, but our code says user_id. Either way, the definition and point is quite clear. We&#8217;re basically just forcing the user to be a valid, logged in user to access and use our application. Once validated, the method just returns us their User ID which we will use now to get whatever information we can that facebook can offer to our application.</p>
<p>In our example, we are going to get their name using the FBML and their friends list using an API call.</p>
<p>We&#8217;ll first examine the FBML call works to get the current user&#8217;s name and information.</p>
<p>If you know anything about XML (or any other markup language), you&#8217;ll see why I said XML is good to know to understand FBML as the syntax is very similar.</p>
<p>We&#8217;re basically creating a tag, and applying some values to it. However, while the syntax may appear the same, it&#8217;s purpose and results are very different.</p>
<p>To understand this piece of code, we&#8217;ll again resort to Facebook&#8217;s Wiki. Instead of client libraries, we&#8217;ll just scroll down to Reference and click the FBML link.</p>
<p>On this page, we&#8217;ll do a simple search for fb:name (since this is the tag we just ran into), and we&#8217;ll click the link it&#8217;s associated with.</p>
<p>As you can see, Facebook does a really good job at explaining this simple (yet complex) markup they have created. While the complex part of it will make it difficult to just jump in head first and try to swim, the Wiki they have provided us with simplifies it (and all the other available FBML tags) drastically to take out a lot of uncertainty as to what we need and don&#8217;t.</p>
<p>You&#8217;ll notice that in the Attributes section for fb:name, there is a table structure for the possible definitions: Required, Name, Type, and Description.</p>
<p>Required determines what is absolutely necessary to use this FMBL tag. Optional, of course, means you don&#8217;t really have to use it, but can be used to obtain whatever it is that tag can obtain with the required field.</p>
<p>In our code, we are only concerned right now with <strong>uid</strong> and <strong>useyou</strong>.</p>
<p>The uid is (as one might expect), the actual user id we received from our initial require_login api call.</p>
<p>The &#8220;useyou&#8221; definition can at first glance not be so easy to understand. It basically says &#8220;if the uid you are passing is equal to the current user who is accessing your facebook application, then show the word YOU instead of the user&#8217;s actual name.&#8221;. So, since our setting is false, we can assume we&#8217;ll see the actual display name of the required UID.</p>
<p>To prove this point, we&#8217;ll test out this definition by saving our changes and opening the url to our application (http://apps.facebook.com/oinkpug/). You&#8217;ll notice on this page, we see Hello, Jonathon Hibbard!</p>
<p>So, let&#8217;s see if we can make it say &#8220;you&#8221; by switching the false to true in the FBML tag fb:name.</p>
<p>Notice when we refresh the page, we indeed see &#8220;you&#8221;. We also can notice that either you or the user&#8217;s name is a link, which directs you to their profile page.</p>
<p>See how simple this fbml really is? It&#8217;s a simple matter of going back to facebook&#8217;s wiki, finding a tag that we want to either use or learn about, and just clicking and reading.</p>
<p>OK, let&#8217;s continue. On line 22, we&#8217;ll notice there is the following call: $facebook-&gt;api_client-&gt;friends_get();</p>
<p>Obviously $facebook is our facebook object, but what is api_client? If we open facebook.php, we&#8217;ll see right away that api_client is defined as a public variable, and if we scroll down to the construct of the facebook class, we&#8217;ll notice that public variable is being loaded as the FacebookRestClient object (which I spoke about earlier that is basically just the nuts and bolts of accessing the specific facebook data via JSON).</p>
<p>So, we now know that we&#8217;re basically just calling the FacebookRestClient object and looking for the method friends_get();</p>
<p>This time, when we view the code for the FacebookRestClient class and search for the friends_get method, we&#8217;ll be presented with a very nice description of just what it does.</p>
<p>Notice the pattern? We refer to the class files first for their definition, and then if we can&#8217;t find a valid definition, we resort to the facebook wiki. And if the wiki fails (which it is very possible) to give us the information we need, there is always the greatest tool on the Internet:  <a href="http://www.google.com" target="_blank">google</a>.</p>
<p>But this time, we get exactly what we want. The definition and meaning behind the method we&#8217;re calling.</p>
<p>Now that we have this, let&#8217;s switch back to our index.php file.</p>
<p>The next few lines may or may not be very familiar to most. Either way, here&#8217;s what&#8217;s going on:</p>
<p>While our $friends array, we&#8217;re going to grab only the first 25 friends. Then, we&#8217;re going to loop through these friends and display each key&#8217;s value.</p>
<p>When we view our application page again, this time we&#8217;ll notice exactly what that list of numbers was: user id&#8217;s.</p>
<p>But what can we do with these id&#8217;s? How do we get the actual human readable names that correspond to each of this id&#8217;s?</p>
<p>Instead of complicating this question more by doing a google search, we must first look for the most simple answer that&#8217;s right in front of us. Can anyone guess?</p>
<p>We can do it by using the FBML tag we used to obtain the currently logged in user&#8217;s name in the first place.</p>
<p>So, for echo &#8220;&lt;br /&gt;$friend&#8221;;, let&#8217;s replace it with a FBML call!</p>
<p>echo &#8220;&lt;br /&gt;&lt;fb:name uid=\&#8221;$friend\&#8221; /&gt;&#8221;;</p>
<p>When we refresh our page now, we&#8217;ll get the list of names we were looking for!</p>
<p>So, what if we actually wanted to do a bit more, like save changes, or have the user input some information for us?</p>
<hr />
<h3>VI. How to setup a basic DB-driven application to be used with Facebook&#8217;s API</h3>
<p>This final section is probably the most complex of them all. While we only have a very small understanding of Facebook&#8217;s API model and the FBML, creating a DB-Driven application can get very complicated quickly, thus resulting in an overwhelming feeling that you&#8217;ll never get it. Then again, maybe it&#8217;ll be very easy for you. Either way, each person is different, but the thing you have to keep remembering is that this is nothing more than just one more thing you&#8217;re trying to learn to better yourself as not only a developer, but a current/future employee. Facebook is gaining a LOT of attention, and saying that you know (or are in the process of learning) their API model should impress your employer (or future employer) at the very least.</p>
<p>While I could go on a huge tangent about all this, we&#8217;ll instead just do what developers do to learn something foreign: jump in head first.</p>
<p>Thankfully though, once again Facebook has provided us with an example of creating a simple DB and event driven application which i mentioned earlier: footprints. There does, however, exist a problem with this example (which has yet to be fixed), but I&#8217;ve already commented the fix in the code (which we&#8217;ll get to after seeing the error).</p>
<p>For now, let&#8217;s just open the directory and see what there is to see.</p>
<pre>cd apps/classes/facebook/footprints</pre>
<p>Inside this directory you&#8217;ll see 4 files: config.php, index.php, lib.php and README</p>
<p>Since we&#8217;re new to all of this, let&#8217;s first look at the README fle. Once again we&#8217;ll see that the complexity of this API (which we talked about earlier) is met with simplicity from Facebook&#8217;s explanation of what exactly we&#8217;re looking at here.</p>
<p>So we know that, first, we&#8217;ll want to open the config.php and update it with the necessary database connection information. We&#8217;ll also have to either get our API/Secret keys from the application settings page, or we can just copy it directly from our existing index.php file.</p>
<p>When examining config.php, we&#8217;ll notice there also exists a CREATE TABLE definition as the README indicated. I&#8217;ve already created this table (along with the database of oinkpug), so we&#8217;ll just skip this step.</p>
<p>Once our config.php file is setup and saved, we now need to determine how we want to test this application out.</p>
<p>There exists 2 methods: 1) we can create a whole new application on facebook along with a directory on our hosting server, or 2) we can just backup index.php in our current oinkpug directory and copy the files in footprints into that base directory.</p>
<p>For this presentation, we&#8217;ll just use method 2 as it&#8217;s quicker and easier.</p>
<p>Now that we have the files backed up and copied over, let&#8217;s open up our new index.php file and see exactly what&#8217;s happening in this example.</p>
<p>First, we&#8217;ll notice that we&#8217;ll have to update the require_once to point at our actual path to the facebook.php class file.</p>
<p>We&#8217;ll also see that there are 2 more includes: lib.php and config.php</p>
<p>The lib.php file is basically a list of methods that we&#8217;ll be using for this page, along with it&#8217;s actions (we&#8217;ll visit this file real soon). And of course, we already know that the config.php file holds our keys and database connection information.</p>
<p>As we move down the file, we notice our now familiar instantiation of the facebook object, but instead of immediately requre_login we have a new method call: require_frame();</p>
<p>While Facebook&#8217;s class definition doesn&#8217;t really explain what this function is/does and the facebook wiki doesn&#8217;t help either, we have to resort to google for an explanation.</p>
<p>A quick search will lead us to the facebook forums which has a few posts on the subject. In a nutshell, the require_frame() function basically says that you must be inside facebook to execute this application. In other words, if we try to just access our application via url (http://www.phpadvocate.com/oinkpug) you&#8217;ll notice we get directed immediately to the canvas url of this application. The catch to this is we don&#8217;t really even need this, as require_login does the same (plus returns the user&#8217;s id), but for the sake of argument, we&#8217;ll leave it in.</p>
<p>Next we see that we&#8217;re checking for a posted value to exist named To. We also notice 2 functions that are being called from lib.php : do_step and get_prints.</p>
<p>Skimming down to line 29, we&#8217;ll notice that we&#8217;re opening up a div with some basic HTML and then also find our already visited fb:name FBML tag. The very next line calls yet another new method: get_add_url. This function basically just gives the user a link to add your application to their profile if it doesn&#8217;t already exist.</p>
<p>After this, we find a small if statement that is checking a $prints_id to our $user id.</p>
<p>When/If our IF statement rings true, we see that we&#8217;re presented with a simple FB:NAME FBML tag again that&#8217;s asking if we want to step on a user which appears to be a confirmation question.</p>
<p>When our IF statement rings false, we notice a brand new FBML tag: fb:friend-selector. This FBML tag (as we will see in just a second) is just one of MANY Ajax-controlled tags that presents us with a dynamically driven look up for searching our friends list for names.</p>
<p>Next, we&#8217;ll see our familiar FB:NAME FBML tag, but this time with a new attribute inside of it: possessive. This tag basically works as the useyou attribute, only possessive displays the word YOUR.</p>
<p>Finally, we see our last method call: render_prints() which we will learn about once we view the lib.php file.</p>
<p>So now, let&#8217;s just load up our application. When we do, we&#8217;re presented with a nice simple form, and we&#8217;ll notice the new search box that&#8217;s in the middle of the page. This is what makes the FBML such a lovable markup. They take all the hard work of creating an Ajax-driven search box that looks like Facebook&#8217;s environment, and makes it available to you in one simple line of code!</p>
<p>However, after we type in the name we are looking up, and press Step, we notice right away there are 2 errors that show up:</p>
<pre>Warning: Missing argument 3 for FacebookRestClient::notifications_send(), called in /oinkpug_101/lib.php on line 52 and defined in /oinkpug_101/apps/classes/facebook/php/facebookapi_php5_restlib.php on line 1450

Fatal error: Call to undefined method FacebookRestClient::feed_publishActionOfUser() in /oinkpug_101/lib.php on line 58</pre>
<p>WTF right? Well, this is actually not your fault here, this is an issue with Facebook&#8217;s example having errors and outdated code. You can view this <a href="http://forum.developers.facebook.com/viewtopic.php?id=27548">this forum page</a> for a fix, or just follow these steps:</p>
<ul>
<li>Open lib.php</li>
<li>Find the function do_step (which is causing all of our problems&#8230;)</li>
<li>Add a string as the 3rd paramater for the $facebook-&gt;api_client-&gt;notifications_send call (ie: <strong>$result = &amp; $facebook-&gt;api_client-&gt;notifications_send($to, &#8216; stepped on you. &#8216; . &#8216;&lt;a href=&#8221;http://apps.facebook.com/footprints/&#8221;&lt;See all your Footprints&lt;/a&gt;.&#8217;);</strong> becomes <strong>$result = &amp; $facebook-&gt;api_client-&gt;notifications_send($to, &#8216; stepped on you. &lt;a href=&#8221;http://apps.facebook.com/footprints/&#8221;&gt;See all your Footprints&lt;/a&gt;.&#8217;), &#8216;app_to_user&#8217;;</strong>);</li>
<li>Finally, replace the function call <strong>$facebook-&gt;api_client-&gt;feed_publishActionOfUser</strong> to be <strong>$facebook-&gt;api_client-&gt;feed_publishUserAction</strong>.  The reason is publicActionOfUser no longer exists in the API and was instead replace with publishUserAction&#8230;</li>
<li>Save and refresh your page. Presto! Fixed.</li>
</ul>
<p>Now, if we go through this lib.php file, we will start to see all of our very common SQL calls, also new FBML calls, and even some new FB API calls! Will I go into them now? I doubt time has spared us that much. So the ball is now in your court to do the wonderful world of discovery against this great API. After all, we just scratched the service, and where better to pick up than from a simple example?</p>
<p>Good luck, and I hope this presentation has been helpful in some way.</p>
<hr />
<h3>VII. References</h3>
<p><a href="http://wiki.developers.facebook.com/index.php/FBML " target="_blank">http://wiki.developers.facebook.com/index.php/FBML </a>- FBML Introduction / Usage List<br />
<a href="http://wiki.developers.facebook.com/index.php/Category:FBML_tags" target="_blank">http://wiki.developers.facebook.com/index.php/Category:FBML_tags</a> &#8211; Alphabetical Listing of all FBML Tags (click one to get more information)<br />
<a href="http://wiki.developers.facebook.com/index.php/FQL" target="_blank">http://wiki.developers.facebook.com/index.php/FQL</a> &#8211; FQL Introduction / Usage List<br />
<a href="http://wiki.developers.facebook.com/index.php/FQL_Tables" target="_blank">http://wiki.developers.facebook.com/index.php/FQL_Tables</a> &#8211; FQL Table Schemas..<br />
<a href="http://wiki.developers.facebook.com/index.php/User:Client_Libraries" target="_blank">http://wiki.developers.facebook.com/index.php/User:Client_Libraries</a> &#8211; PHP Client Library explanations/help<br />
<a href="http://forum.developers.facebook.com/" target="_blank">http://forum.developers.facebook.com/</a> &#8211; The Facebook API Forums<br />
<a href="http://www.google.com/search?hl=en&amp;q=facebook+php+api+tutorial&amp;start=10&amp;sa=N" target="_blank">http://www.phpeveryday.com/articles/Facebook-Programming-My-First-Facebook-Application-P847.html<br />
</a><a href="http://forum.developers.facebook.com/viewtopic.php?id=27548" target="_blank">http://forum.developers.facebook.com/viewtopic.php?id=27548</a> &#8211; BUG FIX FOR FOOTPRINTS EXAMPLE CODE</p>
]]></content:encoded>
			<wfw:commentRss>http://phpadvocate.com/blog/?feed=rss2&amp;p=142</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using declare and ticks with PHP</title>
		<link>http://phpadvocate.com/blog/?p=102</link>
		<comments>http://phpadvocate.com/blog/?p=102#comments</comments>
		<pubDate>Tue, 19 Jan 2010 14:53:22 +0000</pubDate>
		<dc:creator>Jonathon Hibbard</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[declare construct]]></category>
		<category><![CDATA[ticks]]></category>

		<guid isPermaLink="false">http://phpadvocate.com/blog/?p=102</guid>
		<description><![CDATA[Today I want to talk about a construct many have never used, nor heard of: declare. This construct, when used in conjunction with ticks and register_tick_function, is an extremely powerful and useful tool. If one were to read the definition and usage on the manual, it&#8217;s usage is not immediately clear. But don&#8217;t worry, it [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://phpadvocate.com/blog/wp-content/uploads/2010/01/tick-11.jpg"><img class="alignleft size-thumbnail wp-image-104" title="tick" src="http://phpadvocate.com/blog/wp-content/uploads/2010/01/tick-11-150x150.jpg" alt="" width="150" height="150" /></a>Today I want to talk about a construct many have never used, nor heard of: <a href="http://www.php.net/declare">declare</a>. This construct, when used in conjunction with <a href="http://www.php.net/manual/en/control-structures.declare.php#control-structures.declare.ticks">ticks</a> and <a href="http://www.php.net/manual/en/function.register-tick-function.php">register_tick_function</a>, is an extremely powerful and useful tool. If one were to read the definition and usage on the manual, it&#8217;s usage is not immediately clear. But don&#8217;t worry, it even took me about 2 hours to finally see just <em>what</em> exactly this would be useful for.</p>
<p>With that in mind, today&#8217;s article will be on <strong>ticks</strong>, what they are, how to use them, and how they will benefit you. Keep in mind that this is a more advanced tutorial and some may have trouble understanding all these usages (or even the importance), but I&#8217;ll try my best to explain so everyone has at least a more solid understanding of this great construct.</p>
<p><strong>What is a Tick?</strong></p>
<p>Despite the introductory image, ticks in PHP are not evil insects looking to suck the life out of you. But before we can understand what a tick is, we must understand what a <a href="http://www.php.net/declare">declare construct</a> is, which our &#8220;ticks&#8221; are created with. The PHP manual defines it as follows:</p>
<blockquote><p>The <em>declare</em> construct is used to set execution directives for a block of code. The syntax of <em>declare</em> is similar to the syntax of other flow control constructs:</p>
<div>
<div>
<div>
<pre>declare (directive)
    statement</pre>
</div>
</div>
</div>
<p>The <em>directive</em> section allows the behavior of the <em>declare</em> block to be set. Currently only two directives are recognized: the <em>ticks</em> directive (See below for more information on the <a href="http://www.php.net/manual/en/control-structures.declare.php#control-structures.declare.ticks">ticks</a> directive) and the <em>encoding</em> directive (See below for more information on the <a href="http://www.php.net/manual/en/control-structures.declare.php#control-structures.declare.encoding">encoding</a> directive).</p>
<blockquote><p><strong>Note</strong>: The encoding directive was added in PHP 5.3.0</p></blockquote>
<p>The <em>statement</em> part of the <em>declare</em> block will be executed &#8211; how it is executed and what side effects occur during execution may depend on the directive set in the <em>directive</em> block.</p>
<p>The <em>declare</em> construct can also be used in the global scope, affecting all code following it (however if the file with <em>declare</em> was included then it does not affect the parent file).</p></blockquote>
<p>If you are scratching your head trying to understand this, you are not alone. While the above makes sense once you <em>realize</em> what declare really is, but being able to actually undestand it can be challenging.  To explain this in a bit more detail, let&#8217;s take a look at an example of code other than what the PHP manual wants to show us.</p>
<p><strong>Note: This example is only 1 of 2 methods to use the &#8220;declare&#8221; construct.  I will explain the second later in this article.  For now, we&#8217;ll use the easiest to understand.</strong></p>
<pre>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:800px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">function</span> my_callback_function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;I was called from a tick!&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">declare</span><span style="color: #009900;">&#40;</span>ticks <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/register_tick_function"><span style="color: #990000;">register_tick_function</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my_callback_function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
</pre>
<p>The above example consists of 4 major parts: a function definition, a &#8220;declare&#8221; construct, a &#8220;register_tick_function&#8221; call, and a simple variable assignment.  The first thing we&#8217;re going to talk about though is the &#8220;declare construct&#8221;.</p>
<p>In our example, we have the follwing bit of code:</p>
<pre>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:800px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">declare</span><span style="color: #009900;">&#40;</span>ticks <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
</pre>
<p>This sets the stage for how many lines of actual PHP code we are going to tick (or, for a better use of the word, count).  The definition &#8220;ticks = 1&#8243; tells PHP that for every single bit of PHP code that it can &#8220;tick&#8221; (aside from condition expressions and argument expressions), it will keep a record of each one.</p>
<p>But before we can make use of these ticks, we should define a function that will keep track and use each tick that we encounter (you are not required to create a call-back function, though it is suggested to do so since you&#8217;re using the declare construct for a reason..).  We do this with the following piece of code:</p>
<pre>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:800px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://www.php.net/register_tick_function"><span style="color: #990000;">register_tick_function</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my_callback_function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
</pre>
<p>Already, our callback function will have been called, resulting in the line &#8220;I was called from a tick!&#8221;, which means 1 tick has been executed.  This part is a bit confusing, but the point is that once we issue &#8220;declare&#8221;, PHP will start &#8220;ticking&#8221; the very next line on until we stop the declare from working or PHP ends, whichever comes first.</p>
<p>You will notice 2 more &#8220;I was called from a tick!&#8221; statements on your browser.  The second echo statement should be obvious since PHP found our $x = 10; statement, resulting in the next tick.  But where is this third tick coming from?  If you guessed the close tag of PHP (?&gt;) you would be correct!  PHP will report ticks all the way down to the very last PHP code (in this case, the closing ?&gt; tag)!</p>
<p>In the note above, it is indicated that we can use ticks 2 different ways (including the first example shown above).  Here is the other example to get <em>almost</em> the exact same result:</p>
<pre>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:800px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">function</span> my_callback_function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;I was called from a tick!&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<a href="http://www.php.net/register_tick_function"><span style="color: #990000;">register_tick_function</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my_callback_function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">declare</span><span style="color: #009900;">&#40;</span>ticks <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
<span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
</pre>
<p>If we execute this piece of code and observe the output to our browser, we&#8217;ll notice that instead of 3 &#8220;I was called from a tick!&#8221; messages, we&#8217;ll only see 2!  Why?  Because even though we &#8220;registered&#8221; our callback function for the declare construct, we hadn&#8217;t actually <em>called</em> the declare construct!  In other words, PHP will not start &#8220;ticking&#8221; until we actually call the declare construct, but will even issue a tick for our closing brace in this declaration, just like it would issue one for our ending PHP tag.</p>
<p>A question would be, though, &#8220;Why is there not another &#8220;I was called from a tick!&#8221; to account for the opening curly brace?&#8221;.  That&#8217;s because, in this last example, the opening curly brace is what actually tells PHP to start crunching code, not the definition alone by itself.</p>
<p>Before we get any further into this subject, I want to show you all something that will &#8220;almost&#8221; make everything you understand so far seem to fly right out the window.  So why am I showing it to you?  So that you understand how PHP operates.  Note this example:</p>
<pre>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:800px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">function</span> my_callback_function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;I was called from a tick!&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<a href="http://www.php.net/register_tick_function"><span style="color: #990000;">register_tick_function</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my_callback_function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">declare</span><span style="color: #009900;">&#40;</span>ticks <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
</pre>
<p>Before we even execute this, one would assume that we would never see &#8220;I was called from a tick!&#8221;.  That is not the case.  You will actually see it <strong>3 times</strong>!  What is going on here?  I asked the exact same question so I turned to lots of articles, bug reports, and PHP forums to find out this is, indeed, a bug with the declare construct.  But seeing as how this is a very small bug (and one that really won&#8217;t be discovered in your scripts unless you just blatantly leave the code blank as above), it shouldn&#8217;t be that big of a deal.</p>
<p>I hope this has been a useful post for you.  The potential of ticks is huge, and the results offer a great insight into debugging and code management.  While I do not condone using this method in production, it is definitely a method to consider when in a staging/testing environment.</p>
]]></content:encoded>
			<wfw:commentRss>http://phpadvocate.com/blog/?feed=rss2&amp;p=102</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Optimizing Tips for PHP Part IV &#8211; By Example (Part I)</title>
		<link>http://phpadvocate.com/blog/?p=53</link>
		<comments>http://phpadvocate.com/blog/?p=53#comments</comments>
		<pubDate>Tue, 12 Jan 2010 04:34:42 +0000</pubDate>
		<dc:creator>Jonathon Hibbard</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Simple Optimizing Tips for PHP]]></category>
		<category><![CDATA[optimize php]]></category>
		<category><![CDATA[recursive loop]]></category>

		<guid isPermaLink="false">http://phpadvocate.com/php/?p=53</guid>
		<description><![CDATA[As we write PHP code (or any language for that matter) we begin to start thinking about optimizing code. But even though our intentions are for &#8220;the greater good&#8221;, we sometimes miss things that should be obvious. Today&#8217;s article takes a look at: By Example All developers progress at their own rate. As we progress, [...]]]></description>
			<content:encoded><![CDATA[<p>As we write PHP code (or any language for that matter) we begin to start thinking about optimizing code. But even though our intentions are for &#8220;the greater good&#8221;, we sometimes miss things that should be obvious.</p>
<p>Today&#8217;s article takes a look at:</p>
<h3>By Example</h3>
<p><img class="alignleft" title="Example" src="http://www.phpadvocate.com/images/symbol-coding-example.jpg" alt="" width="287" height="320" />All developers progress at their own rate. As we progress, a few things that become evident is the existence of built-in php functions that can replace many of our own custom code, simply because we didn&#8217;t know any other way. We also learn how to see repetition in our code and how to simplify it, when we should separate functionality into their own functions (or even classes!), and also just *know* when something will or will not work from personal experience. During this phase in life, we&#8217;re becoming a senior developer.</p>
<p>Before we get to this point though, we are intermediate developers. Intermediate developers (in my honest opinion) is where a LOT of innovation comes from. It&#8217;s from this level of developer that they are exploring PHP&#8217;s full potential and how to exploit it to their benefit. When we are reaching the end of intermediate level and are looking to become the senior level developer on our team, we begin to try to search for ways to better ourselves as a developer. Unfortunately, such tutorials are very scarce and hard to find.</p>
<p>This tutorial is not to be the end-all be-all, but it is intended to help an intermediate find that next level. And we do this by examples.</p>
<p>Since even simple examples can lead to a book&#8217;s worth of information, I plan to break this 4th part of <a href="http://phpadvocate.com/blog/?p=53">Simple Optimizing Tips for PHP</a> into sub parts. This will help reduce the amount of time it takes you to read this article, but also not try to slam too much information into your brain. Please keep in mind that while some of these examples won&#8217;t exactly speed php&#8217;s performance up, they will instead inadvertently teach you new methods and in turn try to warp your mind into a more sophisticated developer.</p>
<p>So without further ado, let&#8217;s begin!</p>
<p>Our first example is the following function:</p>
<pre>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:800px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> multi_array_search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$the_array</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$the_array</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$the_array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> multi_array_search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array_unshift"><span style="color: #990000;">array_unshift</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$return</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #000088;">$return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$return</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_value</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$the_array</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</pre>
<p>This function is perfectly valid, and works exactly as intended. However, there are a few things that one should notice right away:</p>
<ol>
<li>The spacing and tabing of the data is not lined up nor consistant, making it hard to read.</li>
<li>The function is not taking advantage of the typehints that are now available in php 5.</li>
<li>Some of the code is a bit long and can be reduced to smaller lines without obfuscating the code.</li>
<li>The naming of some of the variables could be redefined to make understanding their purpose easier.</li>
<li>There exists absolutely no commenting in this function to explain it&#8217;s purpose.</li>
<li>The possible return value is either boolean (true AND false) OR array!</li>
<li>The 2 parts of this function (the first if and the following else statements) could be refactored to make more sense and be more useful.</li>
<p>So now that we know this, how can we make this function better? Here is a revised, optomized solution.</p>
<pre>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:800px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009933; font-style: italic;">/**<br />
* @author John Doe<br />
*<br />
* Takes an array and searching for a value within.<br />
*<br />
* @param string $search_value<br />
* @param array &nbsp;$search_array<br />
* @return boolean &nbsp;// Returns true if a search value found, false if not.<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">function</span> multi_array_search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_value</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a> <span style="color: #000088;">$search_array</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;<span style="color: #000088;">$search_result</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># We have an array, so let's recursively search the subarray values<br />
</span> &nbsp; &nbsp; <span style="color: #000088;">$search_result</span> <span style="color: #339933;">=</span> multi_array_search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_value</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array_unshift"><span style="color: #990000;">array_unshift</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># We have a string, so let's compare and see if we have a match<br />
</span> &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$search_value</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp;<span style="color: #b1b100;">return</span> <span style="color: #000088;">$search_result</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</pre>
<p>Notice that we are using the recursive method that was explained in my <a href="http://phpadvocate.com/blog/?p=41">second tutorial</a> on recursively looping through arrays. Also, we are now only returning a boolean value from this point on. In the case that we find a value, we return only a true, otherwise (and by default) we return false.</p>
<p>Now the above may or may not run faster (I haven&#8217;t exactly run a test on this), but it is more simple, less code, and more understandable with comments that guide the way.</p>
<p>For the second part of <a href="http://phpadvocate.com/blog/?p=53">By Example</a>, we will be looking at redundant functionality and definitions, and learn how to better adjust our code to be more useful rather than bloated.</ol>
]]></content:encoded>
			<wfw:commentRss>http://phpadvocate.com/blog/?feed=rss2&amp;p=53</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
