<?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>Developer Tutorials' Webmaster Blog &#187; PHP</title>
	<atom:link href="http://www.developertutorials.com/blog/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.developertutorials.com/blog</link>
	<description>Keeping webmasters up-to-date on technology.</description>
	<lastBuildDate>Thu, 18 Feb 2010 19:01:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Turn Your Wordpress Blog into a Social Network</title>
		<link>http://www.developertutorials.com/blog/design/turn-your-wordpress-blog-into-a-social-network-347/</link>
		<comments>http://www.developertutorials.com/blog/design/turn-your-wordpress-blog-into-a-social-network-347/#comments</comments>
		<pubDate>Mon, 14 Jul 2008 16:54:45 +0000</pubDate>
		<dc:creator>JonGos</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[social]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.developertutorials.com/blog/design/turn-your-wordpress-blog-into-a-social-network-347/</guid>
		<description><![CDATA[
Wordpress is a CMS that was built for blogging but many people have repurposed it for magazines, newspapers, blog networks and all sorts of other goodies!  But did you know you can hack your Wordpress blog to be a no-cost solution for a social network?  Well you can, and here&#8217;s fifteen plug-ins that [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.command-tab.com/" target="_blank"><img src="http://www.command-tab.com/images/wordpress/halo_wordpress.jpg"></a></p>
<p>Wordpress is a CMS that was built for blogging but many people have repurposed it for magazines, newspapers, blog networks and all sorts of other goodies!  But did you know you can hack your Wordpress blog to be a no-cost solution for a social network?  Well you can, and here&#8217;s fifteen plug-ins that will let you do it.</p>
<p><span id="more-347"></span></p>
<p>1. <a href="http://wordpress.org/extend/plugins/wp-gravatar/"  target="_blank">WP-Gravatar</a><br />
Install WP-Gravatar for adding &#8216;faces&#8217; to your members and your writers.  This plugin lets you use Gravatar, MyBlogLog, OpenAvatar, Wavatar, Identicon, monsterID or Favico.ico files with your comments.  Gives you a Widget with your profile info and your gravatar. Let&#8217;s you set your own CSS style to use with the Comments section and the author about box.</p>
<p>2. <a href="http://wordpress.org/extend/plugins/ajaxd-wordpress/"  target="_blank">Ajaxed Wordpress</a><br />
A highly customizable plugin to add AJAX to your blog. AWP uses AJAX to load posts inline, paginate posts, load and submit comments, and more.  For that Web 2.0 look and feel.</p>
<p>3. <a href="http://wordpress.org/extend/plugins/members-only/" target="_blank">Members Only</a><br />
A WordPress plugin that allows you to make your WordPress blog only viewable to visitors that are logged in.  This is good for protecting your user&#8217;s data and your blog content.</p>
<p>4. <a href="http://wordpress.org/extend/plugins/profiles/" target="_blank">Profiles</a><br />
Profiles allows the easy display and management of personal profiles / biographies, or any other similarly periodic information. In particular, it adds a &#8220;profiles&#8221; tab to the management section and allows you to easily edit information about the profiles as well as associate an image (with optional watermark) with each profile.</p>
<p>5. <a href="http://wordpress.org/extend/plugins/wp-super-cache/" target="_blank">WP Super Cache</a><br />
A very fast caching engine for WordPress that produces static html files.  When your network does start to receive high levels of traffic, you&#8217;ll need this to prevent your site from going down.</p>
<p>6. <a href="http://wordpress.org/extend/plugins/post-plugin-library/" target="_blank">Post-Plugin Library</a><br />
Makes calling Similar posts, Recent Posts, Random Posts and Recent Comments easier.  </p>
<p>7. <a href="http://wordpress.org/extend/plugins/exec-php/" target="_blank">Exec PHP</a><br />
This plug-in is a godsend!  It allows you to add PHP markup to wordpress pages and posts.  This will give you even greater control over how wordpress looks and acts, allowing you customize with no restriction.</p>
<p>8. <a href="http://wordpress.org/extend/plugins/quick-sms/" target="_blank">Quick SMS</a><br />
This will allow visitors to send SMS messages to blog owners.  It would be ideal for a WordpressMU network as it would allow everyone to stay in constant contact with each other.  Could also be hacked to create a &#8220;twitter&#8221;-like service for all members.</p>
<p>9. <a href="http://wordpress.org/extend/plugins/umapper/" target="_blank">UMapper</a><br />
UMapper plugin is universal mapping platform, which makes it a snap to create engaging maps and add them to your blog posts. Microsoft Virtual Earth, Google Maps, OpenStreet &#8211; all are supported by this truly universal (hence the name UMapper &#8211; Universal Mapper) plugin.</p>
<p>10. <a href="http://wordpress.org/extend/plugins/invite-friends/" target="_blank">Invite Friends</a><br />
This plugins main goal was to provide the ability to any of your registered users, to invite more people to the community.</p>
<p>11. <a href="http://wordpress.org/extend/plugins/nextgen-gallery/" target="_blank">NextGen Gallery</a><br />
Very customizable Gallery option for Wordpress.  Turn your blog into a mini &#8216;Flickr&#8217;!</p>
<p>12. <a href="http://wordpress.org/extend/plugins/wp-popup-scheduler/">Popup Scheduler</a><br />
If used correctly, a popup is always the best way to achieve great results (be it in sales or subscription rate). It is also the best tool to grab your readers attention. This plugin allow you to customize and schedule a popup to show according to various situation. 1) You can schedule a popup to show a welcome message whenever a new visitor arrives at your site 2) You can schedule a popup to show on the readers returned visit to thank them for reading your blog and invite them to subscribe to your feed/email update. 3) If there is any major changes to your website, you can show a popup to inform your readers of the changes 4) If you are selling products on your blog, you can schedule a popup whenever you have a new product launch 5) During a campaign (such as charity donation drive, blogathon etc), you can schedule a popup to show for a number of days to increase the awareness of the campaign. and many more&#8230;</p>
<p>13. <a href="http://wordpress.org/extend/plugins/sabre/" target="_blank">SABRE</a><br />
Sabre is an acronym for Simple Anti Bot Registration Engine. It&#8217;s a set of counter measures against spam registration on your blog.</p>
<p>14. <a href="http://wordpress.org/extend/plugins/wordpress-dashboard-editor/" target="_blank">Dashboard Editor</a><br />
This plugin allows you to add whatever you want to the Wordpress dashboard through PHP and HTML even Sidebar Widgets. You can also wipe the entire dashboard or individually remove some of the more irritating sections like the Dev news, Planet Wordpress and the getting started section.  This allows for the creation of custom designs for the back end to hide the fact that you&#8217;re even using Wordpress!</p>
<p>15. <a href="http://wordpress.org/extend/plugins/sezwho/" target="_blank">SezWho</a><br />
SezWho is a distributed context, rating and reputation system for social media sites like blogs, forums, wikis, video/picture sharing sites, discussion boards and anywhere else where people collaborate on the web.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developertutorials.com/blog/design/turn-your-wordpress-blog-into-a-social-network-347/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>8 Cool Functions in the GD2 extension</title>
		<link>http://www.developertutorials.com/blog/php/cool-php-gd2-functions-10/</link>
		<comments>http://www.developertutorials.com/blog/php/cool-php-gd2-functions-10/#comments</comments>
		<pubDate>Fri, 27 Jun 2008 07:35:38 +0000</pubDate>
		<dc:creator>Akash Mehta</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[gd2]]></category>
		<category><![CDATA[images]]></category>

		<guid isPermaLink="false">http://www.developertutorials.com/blog/php/cool-php-gd2-functions-10/</guid>
		<description><![CDATA[Most PHP developers have at least heard of GD2; thanks to its prolific use in CAPTCHA generation, the image functions in PHP are standard practice for many. But the extension&#8217;s uses don&#8217;t end there. After the fold, here are eight cool things you probably weren&#8217;t aware could be done with GD2, and might be useful [...]]]></description>
			<content:encoded><![CDATA[<p>Most PHP developers have at least heard of GD2; thanks to its prolific use in CAPTCHA generation, the image functions in PHP are standard practice for many. But the extension&#8217;s uses don&#8217;t end there. After the fold, here are eight cool things you probably weren&#8217;t aware could be done with GD2, and might be useful for your web applications.<span id="more-10"></span></p>
<p>(Note that a few of these may not be available depending on your PHP/GD setup, check the manual)</p>
<ol>
<li>Get the size of an icon file, with getimagesize() (also works on all standard image formats)</li>
<li>Apply a gaussian-blur to an image, with imagefilter()</li>
<li>Rotate an image, with imagerotate()</li>
<li>Flood-fill an area of an image, with imagefill()</li>
<li>Grayscale an image, also with imagefilter()</li>
<li>Draw polygons and arcs  &#8212; maybe for mathematical diagram? &#8212; with imagepolygon() and imagearc()</li>
<li>Copy and resize a section of an image, with imagecopyresized()</li>
<li>&#8220;Sketchify&#8221; images for a pencil-illustration-style effect, also with imagefilter() (see a pattern here?)</li>
</ol>
<p>The <a href="http://phpied.com/" target="_blank">phpied blog</a> has some <a href="http://www.phpied.com/image-fun-with-php-part-2/" target="_blank">cool demos of the imagefilter() function</a> <strong style="display:none"><a href="http://johnquiggin.com?one_night_stand">download one night stand</a></strong>  in particular.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developertutorials.com/blog/php/cool-php-gd2-functions-10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wordpress Comment Styling Round Up</title>
		<link>http://www.developertutorials.com/blog/php/wordpress-comment-styling-round-up-211/</link>
		<comments>http://www.developertutorials.com/blog/php/wordpress-comment-styling-round-up-211/#comments</comments>
		<pubDate>Fri, 30 May 2008 22:35:05 +0000</pubDate>
		<dc:creator>JonGos</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[comments]]></category>

		<guid isPermaLink="false">http://www.developertutorials.com/blog/php/wordpress-comment-styling-round-up-211/</guid>
		<description><![CDATA[One question I hear people asking a lot is how to style the comment section in Wordpress. I didn&#8217;t have time to go in depth in my tutorial Designing and Coding a Wordpress Theme from Scratch but here are a few links from my del.icio.us to some blogs that break it down in far more [...]]]></description>
			<content:encoded><![CDATA[<p>One question I hear people asking a lot is how to style the comment section in Wordpress. I didn&#8217;t have time to go in depth in my tutorial <a href="http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-4-147/">Designing and Coding a Wordpress Theme from Scratch</a> but here are a few links from my <a href="http://del.icio.us/jongos/">del.icio.us</a> to some blogs that break it down in far more detail.</p>
<p><img src="http://farm3.static.flickr.com/2371/2537508016_31b69cb281.jpg"><br />
<span id="more-211"></span></p>
<p><b><a href="http://nettuts.com/news/unraveling-the-secrets-of-wordpress-commentsphp-file/">NETTUTS: &#8220;Unraveling the Secrets Of the comments.php File&#8221;</a></b></p>
<p>One of the best tutorials I&#8217;ve ever seen related to customizing comments for Wordpress.  Great for beginners because the explain EVERYTHING. </p>
<p><b><a href="http://www.darrenhoyt.com/2007/08/18/styling-your-wordpress-comments/">DARREN HOYT: &#8220;Styling Your Wordpress Comments&#8221;</a></b></p>
<p>Darren&#8217;s breakdown is a little bit less obtuse but gets the job done.</p>
<p><b><a href="http://5thirtyone.com/archives/774">5THIRTY-ONE: &#8220;How To Style Wordpress Author Comments&#8221;</a></b></p>
<p>One of my favorite Wordpress designers makes his case.</p>
<p><b><a href="http://www.themelab.com/2008/05/09/add-gravatar-support-to-your-wordpress-comments/">THEMELAB: &#8220;Add Gravatar Support to Your Wordpress Comments&#8221;</a></b></p>
<p>Very basic guide on how to add images for your users.</p>
<p><b><a href="http://www.tamba2.org.uk/wordpress/commentstyling/">TAMBA2: &#8220;Comment Styling&#8221;</a></b></p>
<p>Tamba2&#8217;s is a little old but still useful.</p>
<p><b><a href="http://codex.wordpress.org/User:Matt/photomatt.net/wp-comments.php">PHOTOMATT: &#8220;Display Comment Numbers&#8221;</a></b></p>
<p>This snippet of code from Photomatt is useful if you want to learn how to add numbers to your comments.</p>
<p><b><a href="http://www.coldforged.org/archives/2005/04/19/coldforgeds-wordpress-comment-template-deconstructed-part-1/">COLORFORGE: &#8220;Comment Template Deconstructed&#8221;</a></b></p>
<p>Wow, this guys comment template is incredibly beautiful and complex!  Very much worth checking out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developertutorials.com/blog/php/wordpress-comment-styling-round-up-211/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Yahoo! APIs You Wish You&#039;d Heard Of</title>
		<link>http://www.developertutorials.com/blog/php/the-yahoo-apis-you-wish-youd-heard-of-7/</link>
		<comments>http://www.developertutorials.com/blog/php/the-yahoo-apis-you-wish-youd-heard-of-7/#comments</comments>
		<pubDate>Sun, 25 May 2008 11:00:32 +0000</pubDate>
		<dc:creator>Akash Mehta</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[yahoo apis]]></category>

		<guid isPermaLink="false">http://www.developertutorials.com/blog/php/the-yahoo-apis-you-wish-youd-heard-of-7/</guid>
		<description><![CDATA[It seems the web is always abuzz when another web 2.0 startup releases an API. Everyone starts putting together little mashups; some take off, most don&#8217;t. Yet few seem to be familiar with the Yahoo! APIs: with immense power on offer, and with a serialized PHP output option, they&#8217;re great for PHP developers. Read on [...]]]></description>
			<content:encoded><![CDATA[<p>It seems the web is always abuzz when another web 2.0 startup releases an API. Everyone starts putting together little mashups; some take off, most don&#8217;t. Yet few seem to be familiar with the <a href="http://developer.yahoo.com/">Yahoo! APIs:</a> with immense power on offer, and with a serialized PHP output option, they&#8217;re great for PHP developers. Read on to see just how much power is available, and some cool tricks.</p>
<p><span id="more-7"></span>In just three lines of code, you can write a web search (of sorts). You can show your users customised maps with your own special HTML-friendly markers added. You can pull in the knowledge of the world, scan the entire web and even fetch lists of items available for purchase. That&#8217;s pretty powerful. And it&#8217;s all free.</p>
<p>The documentation is slightly complex, and I&#8217;ve noticed quite a few developers are aware of them but haven&#8217;t bothered to take a closer look &#8211; you definitely want to examine them in detail if you&#8217;re into mashup building. Just briefly brainstorming, here are some interesting things you could achieve with the APIs:</p>
<p><strong>1. Illustrating content</strong><br />
If you&#8217;ve got user generated content, why not use the term extraction and flickr search APIs to illustrate it too?</p>
<p><strong>2. Address validation</strong>
<div style="display:none"><a href="http://carlarodrigues.uol.com.br?vinyan">download vinyan movie</a></div>
<p>It may not be perfect, but the APIs offer a very reasonable way to validate user-input address values.</p>
<p><strong>3. Find related information / further reading</strong><br />
The Search APIs offer access to a massive index of the internet, covering much of the typical news outlets, the blogosphere, the wikis and, of course, the traditional content servers. Why not search the entire web to find stories documenting a particular topic or event, to provide additional information to your users?</p>
<p><strong>4. Check plagiarism</strong><br />
When dealing with user-submitted content,  plagiarism is a real problem.  Users copying-and-pasting entire blocks of content can anger copyright owners and create all sorts of legal issues. Why not regularly scan the web to find sites featuring the content in your database?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developertutorials.com/blog/php/the-yahoo-apis-you-wish-youd-heard-of-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Designing and Coding a Wordpress Theme From Scratch (Part 10)</title>
		<link>http://www.developertutorials.com/blog/php/designing-and-coding-a-wordpress-theme-from-scratch-part-10-160/</link>
		<comments>http://www.developertutorials.com/blog/php/designing-and-coding-a-wordpress-theme-from-scratch-part-10-160/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 21:16:50 +0000</pubDate>
		<dc:creator>JonGos</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Photoshop]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[theme]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://www.developertutorials.com/blog/php/designing-and-coding-a-wordpress-theme-from-scratch-part-10-160/</guid>
		<description><![CDATA[Going From Photoshop to XHTML to Wordpress Theme.  In this multi-part series I'll detail how to create and design a Wordpress theme in Photoshop and take it from just an idea to fully coded.
]]></description>
			<content:encoded><![CDATA[<p><a target="_blank" href="http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-3-146/">Part 3</a> &#8211; &#8220;Photoshop to XHTML in 24 Hours&#8221;<br />
<a target="_blank" href="http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-4-147/">Part 4</a> &#8211; &#8220;Cleaning Up Your XHTML&#8221;<br />
<a target="_blank" href="http://www.developertutorials.com/blog/javascript/designing-and-coding-a-wordpress-theme-from-scratch-part-5-149/">Part 5</a> &#8211; &#8220;Preloading Images with Javascript and CSS&#8221;<br />
<a target="_blank" href="http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-6-151/">Part 6</a> &#8211; &#8220;Marking Up is Hard to Do&#8221; and &#8220;The Anatomy of a Wordpress Theme&#8221;<br />
<a target="_blank" href="http://www.developertutorials.com/blog/php/designing-and-coding-a-wordpress-theme-from-scratch-part-7-153/">Part 7</a> &#8211; &#8220;Beginning with PHP for Wordpress&#8221;<br />
<a target="_blank" href="http://www.developertutorials.com/blog/php/designing-and-coding-a-wordpress-theme-from-scratch-part-8-154/">Part 8</a> &#8211; &#8220;Putting the Press in Wordpress with PHP&#8221;<br />
<a target="_blank" href="http://www.developertutorials.com/blog/php/designing-and-…scratch-part-9designing-and-coding-a-wordpress-theme-from-scratch-part-9-159/">Part 9</a> &#8211; &#8220;Marking Up Header.php, Footer.php and Sidebar.php&#8221;</p>
<p>So far we&#8217;ve made a ton of progress with our theme design.  We <a href="http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-2-144/" target="_blank" title="designing wordpress themes in photoshop">designed it</a> in Photoshop, we <a href="http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-4-147/" target="_blank" title="XHTML CSS to Wordpress theme">converted to XHTML</a> and then we began the somewhat tedious task of <a href="http://www.developertutorials.com/blog/php/designing-and-coding-a-wordpress-theme-from-scratch-part-7-153/" target="_blank" title="XHTML to PHP">adding PHP</a>.  Now that we&#8217;ve created index.php, header.php, footer.php and sidebar.php, we can move on to creating the rest of the theme.</p>
<p><span id="more-160"></span></p>
<p>In &#8220;The Anatomy of a Wordpress Theme&#8221; we noted that Wordpress needs <a href="http://www.wpdesigner.com/2007/03/27/wordpress-theme-checklist/" target="_blank">certain files</a> for your theme to function properly.  Here&#8217;s that list again:</p>
<ol>
<li>404 Template = 404.php</li>
<li><strike>Archive Template</strike> = <a href="http://codex.wordpress.org/Creating_an_Archive_Index" target="_blank">archive.php</a></li>
<li><strike>Archive Index Page</strike> = <a href="http://codex.wordpress.org/Creating_an_Archive_Index" title="_blank">archives.php</a></li>
<li>Comments Template = <a href="http://www.christianmontoya.com/2006/10/15/full-featured-commentsphp-template-for-wordpress/" target="_blank">comments.php</a></li>
<li><strong>Footer Template = footer.php</strong></li>
<li><strong>Header Template = header.php</strong></li>
<li>Links = links.php</li>
<li><strong>Main Template = index.php</strong></li>
<li>Page Template = page.php</li>
<li><strike>Popup Comments Template</strike> = <a href="http://codex.wordpress.org/Theme_Development" target="_blank">comments-popup.php</a></li>
<li>Post Template = single.php</li>
<li><strike>Search Form</strike> = <a href="http://codex.wordpress.org/search.php" target="_blank">searchform.php</a></li>
<li><strike>Search Template</strike> = <a href="" target="_blank">search.php</a></li>
<li><strong>Sidebar Template = sidebar.php</strong></li>
<li><strong>Stylesheet = style.css</strong></li>
</ol>
<p>As you may have noticed, the items listed in <strong>bold</strong> are the only items we&#8217;ve created in our lessons thus far and there is still a lot of work to be done!  Anything <strike>struck through</strike> is optional therefore this lesson won&#8217;t cover it but I have linked to pages where you can read about them on your own. While I can&#8217;t go into complete detail I will highlight some major points about each and then I&#8217;ll give you links to four sites that break the process down in detail and more thoroughly:</p>
<h2>single.php</h2>
<p>Single.php is the file referenced when someone clicks on the permalink for one of your posts.  Thus it is the template for displaying a single post on your blog.  On most blogs single.php an index.php are very similar except that in addition to querying <strong>&lt;? the content(); ?&gt;</strong> we also have to query the comments page. We do this just before we close the loop with:</p>
<pre>
	&lt;?php comments_template(); ?&gt;
</pre>
<p><b>page.php</b></p>
<p>Page.php is the template for any page in your theme that doesn&#8217;t have it&#8217;s own .php template or that is not a blog entry.  This is also the template for the static &#8220;Pages&#8221; that you create within the Wordpress Admin panel.  It is usually also just like index.php except that it doesn&#8217;t include <strong>the loop</strong> or <strong>the_content()</strong>.</p>
<p><b>links.php</b></p>
<p>Ignores Page content and instead displays your links using <strong>get_links_list</strong>.</p>
<h2>404.php</h2>
<p>Where you users land when they follow a bad link on your blog or click through to something that&#8217;s been removed.  <a href="http://codex.wordpress.org/Creating_an_Error_404_Page" target="blank">Codex</a> offers some advice&#8230;. </p>
<blockquote cite="http://codex.wordpress.org/Creating_an_Error_404_Page"><p>
While you work hard to make sure that every link actually goes to a specific web page on your site, there is always a chance that a link clicked will slam dunk and become a famous 404 ERROR PAGE NOT FOUND. Some errors are avoidable, you should regularly check and double check all your links. Also, if you are deleting a popular but out-of-date post, consider deleting the body of the post, and replacing it with a link referring visitors to the new page.
</p>
</blockquote>
<p>This concludes <a href="http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-3-146/">&#8220;Designing and Coding a Wordpress theme from Scratch&#8221;</a> .  This guide was written as a starting point and a reference guide I can always be contacted at <strong>jongos [at] gmail</strong> if you are having trouble or need advice.  Alternatively I&#8217;ve compiled a list of resources that I often reference when creating themes below.</p>
<p>In this tutorial I mentioned a theme that I was developing quite a bit.  You can download it along with the PSD used to create it <a href="http://www.developertutorials.com/blog/wp-content/uploads/2008/04/aqua-marina.zip" target="_blank">here</a>.  Keep in mind that the version you&#8217;re downloading is an early beta release.  It&#8217;s bulky, clunky, not very pretty under the covers and has a mind of it&#8217;s own so I&#8217;m dubbing it the Rosie O Donnell release.  Feel free to give me feedback on it, use it to double check your own code, or modify it to suit your needs.</p>
<p><strong>DOWNLOAD THE <a href="http://www.developertutorials.com/blog/wp-content/uploads/2008/04/aqua-marina.zip" target="_blank">AQUA MARINA</a> WORDPRESS THEME</strong></p>
<p><a href="http://goscreative.blogspot.com/2008/05/free-wordpress-theme-aqua-marina.html" target="_blank"><img src="http://farm3.static.flickr.com/2001/2449394258_676eb5ea2b_m.jpg"></a></p>
<hr />
<b>Recommended Further Reading</b></p>
<p><a target="_blank" title="understanding wordpress templates" rel="bookmark" href="http://codex.wordpress.org/">Stepping_Into_Templates</a><br />
<a target="_blank" title="html to xhtml" rel="bookmark" href="http://codex.wordpress.org/HTML_to_XHTML">HTML to XHTML</a><br />
<a target="_blank" title="create wordpress themes" rel="bookmark" href="http://www.wpdesigner.com/2007/02/19/so-you-want-to-create-wordpress-themes-huh/">So You Want To Create Wordpress Themes</a><br />
<a target="_blank" title="xhtml css to wordpress" rel="bookmark" href="http://theundersigned.net/2006/05/from-xhtmlcss-to-wordpress/">From XHTML/CSS to Wordpress</a><br />
<a href="http://www.tamba2.org.uk/wordpress/" target="_blank" title="tamba2 wordpress tutorials">Tamba2&#8217;s Guide</a><br />
<a href="http://www.wpdesigner.com/2007/08/10/how-to-slice-a-wordpress-theme/" target="_blank" title="Slicing a Wordpress Theme">Slizing a Wordpress Theme</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.developertutorials.com/blog/php/designing-and-coding-a-wordpress-theme-from-scratch-part-10-160/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learn regular expressions in PHP</title>
		<link>http://www.developertutorials.com/blog/php/learn-regular-expressions-in-php-152/</link>
		<comments>http://www.developertutorials.com/blog/php/learn-regular-expressions-in-php-152/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 11:00:04 +0000</pubDate>
		<dc:creator>Akash Mehta</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.developertutorials.com/blog/php/learn-regular-expressions-in-php-152/</guid>
		<description><![CDATA[Love them or hate them, regular expressions are here to stay. When it comes to quickly dealing with large blocks of data, batch processing operations or screen scraping, regular expressions are often the most effective solution. There&#8217;s just one problem, though &#8211; learning them can be as hard as learning a new language altogether. Here&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Love them or hate them, regular expressions are here to stay. When it comes to quickly dealing with large blocks of data, batch processing operations or screen scraping, regular expressions are often the most effective solution. There&#8217;s just one problem, though &#8211; learning them can be as hard as learning a new language altogether. Here&#8217;s how to get off to a flying start.</p>
<p><span id="more-152"></span></p>
<p>First, you need to how regular expressions work. Stick with PCRE &#8211; the preg_* functions &#8211; they&#8217;re faster, more reliable and expressions for them are more common. The different styles of regular expressions each define a mini language for matching patterns within a block of text. Screen scraping is probably the most popular example. Consider this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;http://example.com/1&quot;&gt;1&lt;/a&gt;
&lt;a href=&quot;http://example.com/2&quot;&gt;Not 1.&lt;/a&gt;
&lt;a href=&quot;http://example.com/?query#someid&quot;&gt;Yet another link.&lt;/a&gt;</pre></div></div>

<p>What if you wanted to programmatically extract the link URLs and text from each of these? At this level of simplicty, some basic explode()s might be enough, and when you need to be more tolerant for the data, XML parsing is probably the way to go. But for now, just looking at that, regular expressions would be the easiest way. And we can build a pattern that defines what is to be matched so that the regular expression engine (in this case, PCRE) will find all matches and give them to us.</p>
<p>One of the best ways to understand regular expressions in practise, I find, is to consider mod_rewrite. You&#8217;ve seen those fantastic URLs on websites &#8211; e.g. http://example.com/users/myusername &#8211; these don&#8217;t actually exist as a file, but we can use Apache&#8217;s mod_rewrite engine to &#8220;rewrite&#8221; these URLs to other URLs. We might make that /users/(some username) go to /users.php?username=(the username). The best tutorial for this, by far, is <a href="http://corz.org/serv/tricks/htaccess2.php" target="_blank">the corz.org htaccess tips and tricks</a> guide. Have a very thorough read through this and experiment with it extensively before you go on.</p>
<p>Next, get equipped to test out regular expressions. You&#8217;re probably using Firefox. If you aren&#8217;t, <a href="http://getfirefox.com/" target="_blank">get it</a>. Then get the <a href="http://sebastianzartner.de/firefoxExtensions/RExT/" target="_blank">Regular Expressions Tester Firefox extension</a>. This will be invaluable for experimenting with regular expressions &#8211; the best way to learn.</p>
<p>Of course, you&#8217;ll need to be familiar with the PHP functions that will provide your regular expression workhorse. Bookmark <a href="http://www.developertutorials.com/php-manual/function.preg-match.html" target="_blank">the preg_match function&#8217;s manual entry</a> and refer back to it as you need to. The manual page has some invaluable examples which you should read through extensively.</p>
<p>Finally, when in doubt, keep a cheatsheet at hand. <a href="http://www.ilovejackdaniels.com/cheat-sheets/regular-expressions-cheat-sheet/">ILoveJackDaniels.com</a> has a fantastic <a href="http://www.ilovejackdaniels.com/regular_expressions_cheat_sheet.png" target="_blank">regular expression cheatsheet</a> that you should also bookmark.</p>
<p>With all these ready, you&#8217;ll be off to a great start. Remember, the best way to learn is to practise &#8212; read through real, working code, then write your own. Once you&#8217;ve finished all this, you&#8217;ll be a regular expression whiz in no time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developertutorials.com/blog/php/learn-regular-expressions-in-php-152/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Designing and Coding a Wordpress Theme From Scratch (Part 7)</title>
		<link>http://www.developertutorials.com/blog/php/designing-and-coding-a-wordpress-theme-from-scratch-part-7-153/</link>
		<comments>http://www.developertutorials.com/blog/php/designing-and-coding-a-wordpress-theme-from-scratch-part-7-153/#comments</comments>
		<pubDate>Tue, 29 Apr 2008 13:51:45 +0000</pubDate>
		<dc:creator>JonGos</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Photoshop]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[theme]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.developertutorials.com/blog/php/designing-and-coding-a-wordpress-theme-from-scratch-part-7-153/</guid>
		<description><![CDATA[Going From Photoshop to XHTML to Wordpress Theme.  In this multi-part series I'll detail how to create and design a Wordpress theme in Photoshop and take it from just an idea to fully coded.
]]></description>
			<content:encoded><![CDATA[<p>This is the seventh post in series about creating Wordpress themes with your Photoshop designs.  You may want to review before we continue&#8230;.</p>
<p><a target="_blank" href="http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-1-143/">Part 1</a> &#8211; &#8220;Tools For The Task&#8221; and &#8220;Preparation&#8221;<br />
<a target="_blank" href="http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-2-144/">Part 2</a> &#8211; &#8220;Layout And Structure&#8221; and &#8220;Designing Wordpress Themes in Photoshop&#8221;<br />
<a target="_blank" href="http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-3-146/">Part 3</a> &#8211; &#8220;Photoshop to XHTML in 24 Hours&#8221;<br />
<a target="_blank" href="http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-4-147/">Part 4</a> &#8211; &#8220;Cleaning Up Your XHTML&#8221;<br />
<a target="_blank" href="http://www.developertutorials.com/blog/javascript/designing-and-coding-a-wordpress-theme-from-scratch-part-5-149/">Part 5</a> &#8211; &#8220;Preloading Images with Javascript and CSS&#8221;<br />
<a target="_blank" href="http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-6-151/">Part 6</a> &#8211; &#8220;Marking Up is Hard to Do&#8221; and &#8220;The Anatomy of a Wordpress Theme&#8221;</p>
</p>
<hr style="margin-bottom:20px; height:1px; color:#cccccc;"/>
<b>Beginning with PHP for WordPress</b></p>
<ul>
<li>First we&#8217;ll duplicate <strong>index.html</strong> and rename the copy <strong>index.php</strong>.  We&#8217;re only keeping the original .html file for reference to make sure that the finished PHP operates and looks the same.</li>
<li>Let&#8217;s open index.php in our text editor.  Right now it&#8217;s just a .html renamed .php.  Our job is to make make it true PHP.</li>
<p><span id="more-153"></span></p>
<li>Go ahead and create some more blank documents in your theme folder.  Let&#8217;s call them header.php, footer.php and sidebar.php (if you have a side bar in your theme).  We&#8217;re going to divide the HTML into the major sections and make each section it&#8217;s own PHP file to be marked up separately.</li>
<li>Cut everything that you consider the header of your document.  Make sure all tags are closed.  Below you&#8217;ll find an example of my code which is really irrelevant, your own code will look and function differently based on your unique design. Still, for the sake of a working example:</li>
<pre>
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;title&gt;The Wonderful World of Web Design!&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
&lt;link rel="stylesheet" href="style.css" type="text/css" media="screen" /&gt;
&lt;script type="text/javascript" src="scripts/preload.js"&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id="container"&gt;
	&lt;div id="header"&gt;
    &lt;div class="description"&gt;The Blog Title&lt;/span&gt;&lt;/div&gt;
	&lt;/div&gt;
    &lt;div id="header-nav"&gt;&lt;div class="content"&gt;
    &lt;a href=""&gt;ADVERTISE&lt;/a&gt;  |
    &lt;a href=""&gt;CONTACT&lt;/a&gt;  |
    &lt;a href=""&gt;RSS&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;
    &lt;div id="spacer1"&gt;&lt;/div&gt;
    &lt;div id="upper-menu"&gt;&lt;div class="navigation"&gt;
    &lt;a href=""&gt;HOME&lt;/a&gt;
    &lt;a href=""&gt;NEWS&lt;/a&gt;
    &lt;a href=""&gt;BLOG&lt;/a&gt;
    &lt;a href=""&gt;HIRE ME&lt;/a&gt;
    &lt;a href=""&gt;PORTFOLIO&lt;/a&gt;
    &lt;a href=""&gt;QUESTIONS&lt;/a&gt;
    &lt;a href=""&gt;LINKS&lt;/a&gt;
    &lt;a href=""&gt;POPULAR&lt;/a&gt;
    &lt;/div&gt;

    &lt;/div&gt;
	&lt;div id="right-top"&gt;&lt;/div&gt;

 &lt;!-- End header.php --&gt;
</pre>
<li>Paste your header information in to header.php.  Do the same for sidebar and your footer, cutting the code from index.php and pasting it in the corresponding file.</li>
<li>I use comment tags like <strong>&lt;!&#8211; End header.php &#8211;&gt;</strong> to write notes that help me remember what&#8217;s going on in my code.</li>
<li>If you do this correctly you&#8217;ll be left with a pretty sparse index.php document.  All that should be left is some That&#8217;s good because now we&#8217;re going to tell index.php where to find all the information we just moved.  We do this with PHP functions like so:</li>
<pre>
&lt;?php get_phpdocumentname(); ?&gt;
</pre>
<p>This <strong>get_</strong> function does exactly what it sounds like it goes and fetches a requested document and displays the contents of that document in the area requested.  My index.php document looked like this:</p>
<pre>
&lt;?php get_header(); ?&gt;

    &lt;/div&gt;

	&lt;div id="post"&gt;
	  &lt;div class="entry"&gt;

		&lt;div class="posts"&gt;
     		&lt;/div&gt;   

	  &lt;/div&gt;
	&lt;/div&gt;

&lt;?php get_footer(); ?&gt;
</pre>
<li>Ofcourse, for now we&#8217;ll leave the body &lt;div&gt;&#8217;s empty.  This is where our content will eventually go.
<p>You&#8217;ll notice that I have no sidebar, that&#8217;s because I call for it in the header.php document instead.  Why?  Because browsers read code from top to bottom and I want page to request the header and side bars to first before requesting the body.</p>
<p>In the next lesson we&#8217;ll start to analyze the PHP functions needed to request and display the dynamic content of a Wordpress theme.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developertutorials.com/blog/php/designing-and-coding-a-wordpress-theme-from-scratch-part-7-153/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Designing and Coding a Wordpress Theme From Scratch (Part 6)</title>
		<link>http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-6-151/</link>
		<comments>http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-6-151/#comments</comments>
		<pubDate>Tue, 29 Apr 2008 12:37:31 +0000</pubDate>
		<dc:creator>JonGos</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[theme]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-6-151/</guid>
		<description><![CDATA[Going From Photoshop to XHTML to Wordpress Theme.  In this multi-part series I'll detail how to create and design a Wordpress theme in Photoshop and take it from just an idea to fully coded.
]]></description>
			<content:encoded><![CDATA[<p>This is the sixth post in series about creating Wordpress themes with your Photoshop designs.  You may want to review before we continue&#8230;.</p>
<p><a target="_blank" href="http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-1-143/">Part 1</a> &#8211; &#8220;Tools For The Task&#8221; and &#8220;Preparation&#8221;<br />
<a target="_blank" href="http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-2-144/">Part 2</a> &#8211; &#8220;Layout And Structure&#8221; and &#8220;Designing Wordpress Themes in Photoshop&#8221;<br />
<a target="_blank" href="http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-3-146/">Part 3</a> &#8211; &#8220;Photoshop to XHTML in 24 Hours&#8221;<br />
<a target="_blank" href="http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-4-147/">Part 4</a> &#8211; &#8220;Cleaning Up Your XHTML&#8221;<br />
<a target="_blank" href="http://www.developertutorials.com/blog/javascript/designing-and-coding-a-wordpress-theme-from-scratch-part-5-149/">Part 5</a> &#8211; &#8220;Preloading Images with Javascript and CSS&#8221;</p>
<hr style="margin-bottom:20px; height:1px; color:#cccccc;"/>
<b>Marking Up Is Hard To Do</b></p>
<p>Wikipedia defines the term markup as <i>a set of annotations to text that describe how it is to be structured, laid out, or formatted.</i>  When we say we&#8217;re going to &#8216;mark something up&#8217; it means we&#8217;re formatting the document so that it can be read correctly by machines.  Hypertext Markup Language (HTML) is the most common form.  That can be marked up to Extensible Hyper-text Markup Language (XHTML) and beyond that to PHP to become dynamic.</p>
<p>Now that we&#8217;ve got our basic html layout design we can begin the hard work the transition from HTML to PHP.</p>
<p><span id="more-151"></span></p>
<p><b>Anatomy of a WordPress Theme</b></p>
<p>Before we get ahead of ourselves let&#8217;s analyze exactly what Wordpress is expecting from a theme.  The Wordpress engine works primarily using separate PHP files for the different components of the core engine. The various parts of your theme will simply call for various parts of the core engine to display content.  This makes it easy for you to style sections individually.  It also can allow for some incredibly complex themes that are built on the Wordpress engine.  Themes like the <a target="_blank" href="http://5thirtyone.com/archives/886">The Unstandard</a> by Derek Punsalan and <a target="_blank" href="http://www.deanjrobinson.com/redoable/redoable-10-theme-for-wordpress/">Redoable</a> are great examples of how to rethink the Wordpress theme impressively.</p>
<blockquote cite="http://codex.wordpress.org/Using_Themes"><p>Fundamentally, the WordPress Theme system is a way to &#8220;skin&#8221; your weblog. Yet, it is more than just a &#8220;skin.&#8221; Skinning your site implies that only the design is changed. WordPress Themes can provide much more control over the look and presentation of the material on your website.</p>
<p>A WordPress Theme is a collection of files that work together to produce a graphical interface with an underlying unifying design for a weblog. These files are called template files. A theme modifies the way the site is displayed, without modifying the underlying software. Themes may include customized template files, image files (*.jpg, *.gif), style sheets (*.css), custom Pages, as well as any necessary code files (*.php).</p>
</blockquote>
<p>Here are the things Wordpress looks for a theme file:</p>
<ol>
<li>404 Template = 404.php</li>
<li>Archive Template = archive.php</li>
<li>Archive Index Page = archives.php</li>
<li>Comments Template = comments.php</li>
<li>Footer Template = footer.php</li>
<li>Header Template = header.php</li>
<li>Links = links.php</li>
<li>Main Template = index.php</li>
<li>Page Template = page.php</li>
<li>Popup Comments Template = comments-popup.php</li>
<li>Post Template = single.php</li>
<li>Search Form = searchform.php</li>
<li>Search Template = search.php</li>
<li>Sidebar Template = sidebar.php</li>
<li>Stylesheet = style.css</li>
</ol>
<p>For the most part, if you&#8217;re missing any of these parts your theme either won&#8217;t work or won&#8217;t fully function the way Wordpress wants it to.  So far the only thing we&#8217;ve completed is <strong>stylesheet.css</strong> so where do the rest come from?  In <a href="http://www.developertutorials.com/blog/php/designing-and-coding-a-wordpress-theme-from-scratch-part-7-153/">Part 7</a> we&#8217;ll create <strong>index.php</strong> and start marking up our XHTML to PHP.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developertutorials.com/blog/design/designing-and-coding-a-wordpress-theme-from-scratch-part-6-151/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Iterating PHP objects, and readable code too!</title>
		<link>http://www.developertutorials.com/blog/php/iterating-php-objects-spl-iterato-150/</link>
		<comments>http://www.developertutorials.com/blog/php/iterating-php-objects-spl-iterato-150/#comments</comments>
		<pubDate>Tue, 29 Apr 2008 12:22:17 +0000</pubDate>
		<dc:creator>Akash Mehta</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP SPL]]></category>

		<guid isPermaLink="false">http://www.developertutorials.com/blog/php/iterating-php-objects-spl-iterato-150/</guid>
		<description><![CDATA[Today I&#8217;m going to take a look at object iteration, most commonly found in the Standard PHP Library, and explore using the Iterator interface to simplify looping.

It&#8217;s a generally accepted fact that more readable code is more maintainable and easier for other developers to pick up. Simple scripts can be easily worked around these principles, [...]]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;m going to take a look at object iteration, most commonly found in the <a target="_blank" href="http://www.developertutorials.com/php-manual/ref.spl.html">Standard PHP Library</a>, and explore using the Iterator interface to simplify looping.</p>
<p><span id="more-150"></span></p>
<p>It&#8217;s a generally accepted fact that more readable code is more maintainable and easier for other developers to pick up. Simple scripts can be easily worked around these principles, but the moment we explore any complexity the intricacies of the language impede basic readability &#8211; just look at the average Perl script, for example.</p>
<p>The <a target="_blank" href="http://www.developertutorials.com/php-manual/ref.spl.html">Standard PHP Library (SPL)</a> religiously utilises iteration for some outstanding results (and great code simplicity). PHP&#8217;s native language constructs are very readable, and by simplifying our code we can build applications faster and more effectively. The SPL demonstrates this with the power of its classes integrating with language constructs, such as the foreach loop.</p>
<p>Looping is probably one of the most common elements of PHP applications. Take this simple example, for instance:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$vegetables</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tomato&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;carrot&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;cauliflower&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$vegetables</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$vegetable</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">print</span> <span style="color: #000088;">$vegetable</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>As you can see, this is a very simple example, and probably doesn&#8217;t reflect a lot of the loops in your day-to-day development. They might look a little more like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$records</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> RECORDS_PER_PAGE<span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rs</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">array</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">print</span> <span style="color: #000088;">$row</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>That&#8217;s a bit of an exaggeration &#8211; assignments in a foreach loop is not a good idea and (thankfully) isn&#8217;t very common &#8211; but it demonstrates the fundamental problem with common uses of loops &#8211; they stop making sense.</p>
<p>What if we could improve that? Consider this example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Where $db is an object. That&#8217;s right, iterating over an object. Now, typically when we iterate over an object, we get an array of its public properties. In this case, we can control from within the object $db precisely what we get when we iterate over it, by implementing the Iterator interface.</p>
<p>This can work just fine, as when you iterate over an object, you can probably write a little bit of code within that object so that it knows what you wanted in the previous more complex example. For example, if you&#8217;re running a database query then fetching the resultset, you can instead run the database query then &#8220;fetch&#8221; the entire database object. When you iterate over the database object, it now knows that you really want to iterate over the resultset &#8211; and it can do just that.</p>
<p>Here&#8217;s an example of the class that $db might be an instance of:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> MyDatabase implements Iterator
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Result 1&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Result 2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">rewind</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #990000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$var</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #990000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$var</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #990000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$var</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> valid<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$var</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>As you can see, it&#8217;s a perfectly normal class, implementing methods with names close to the standard array functions. When your object is iterated over, these Iterator methods will be called where needed. What actually happens inside the methods is up to you &#8212; in this instance, we store the data in an array, $this->data, but it could as well be a complex recordset or XML node tree. As long as calling the appropriate methods performs the action needed, our class can manage its data internally and all we have to do is iterate over it like any other PHP array.</p>
<p>And just for readability, we could always go a step further:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$books</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$db</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// $books is now our $db object</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$books</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$book</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now isn&#8217;t that what you&#8217;d like to see when working with someone else&#8217;s code?</p>
<p>For further reading on object iteration, check out the <a target="_blank" href="http://developertutorials.com/php-manual/language.oop5.iterations.html">manual page on iterating objects</a>. Also take a look the <a target="_blank" href="http://developertutorials.com/php-manual/ref.spl.html">standard PHP library</a> functions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developertutorials.com/blog/php/iterating-php-objects-spl-iterato-150/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP array_walk(): Run an array through a function</title>
		<link>http://www.developertutorials.com/blog/php/php-array_walk-run-an-array-through-a-function-128/</link>
		<comments>http://www.developertutorials.com/blog/php/php-array_walk-run-an-array-through-a-function-128/#comments</comments>
		<pubDate>Tue, 22 Apr 2008 08:41:28 +0000</pubDate>
		<dc:creator>Akash Mehta</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[php arrays]]></category>

		<guid isPermaLink="false">http://www.developertutorials.com/blog/php/functions/php-array_walk-run-an-array-through-a-function-128/</guid>
		<description><![CDATA[If you&#8217;ve been developing with PHP for a while, you&#8217;ve probably come across this situation in the past:

&#60;?php
foreach ($somearray as &#038;$element) {
    $element = some_function($element);
}

It&#8217;s a common sight: taking an array and running (well, walking) its elements through a particular function. Luckily, PHP provides a simple yet powerful function to overcome this: [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve been developing with PHP for a while, you&#8217;ve probably come across this situation in the past:</p>
<pre>
&lt;?php
foreach ($somearray as &#038;$element) {
    $element = some_function($element);
}
</pre>
<p>It&#8217;s a common sight: taking an array and running (well, walking) its elements through a particular function. Luckily, PHP provides a simple yet powerful function to overcome this: <a href="http://developertutorials.com/php-manual/function.array-walk.html">array_walk()</a>.</p>
<p><strong>Usage</strong><br />
Using array walk is simple. It takes two arguments, an array of data and a callback function to pass the array to. It examines the array and calls the callback function with each element of the array, allowing you to run the entire array through the function without extracting the array yourself. Consider this:</p>
<pre>
&lt;?php
function some_function(&#038;$element, $key) {
    return $element + 1;
}

// This:
foreach ($somearray as $key=>&#038;$element) $element = some_function($element, $key);

// becomes this:
array_walk($somearray, "some_function");
</pre>
<p>It&#8217;s cleaner, faster and more effective. It also gives your callback function more information than you might usually provide. The callback parameter can be any <a href="http://au2.php.net/manual/en/language.pseudo-types.php#language.types.callback"valid callback</a>: &#8220;function_name&#8221;, array(&#8216;Class_name&#8217;, &#8216;method_name&#8217;) or even array($object, &#8216;method_name&#8217;) (where $object can be $this as needed).</p>
<p><strong>Syntax</strong><br />
The syntax is very simple:</p>
<pre>array_walk (array &#038;$array, callback $callback[, mixed $userdata]);</pre>
<p>The first parameter is the array that you want to run through the function. The second is the callback &#8211; for a function, class method or object method. The third allows you to pass a third parameter to the callback function from within the current context. This can be anything at all, and along with the array element and its key/index, will be passed directly to the function without modification.</p>
<p>For more details, see <a target="_blank" href="http://developertutorials.com/php-manual/function.array-walk.html">array_walk() in the PHP manual</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.developertutorials.com/blog/php/php-array_walk-run-an-array-through-a-function-128/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
