<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/css/rss20.xsl" type="text/xsl"?>
<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/" xmlns:pheedo="http://www.pheedo.com/namespace/pheedo">
	<channel>
		<title>SitePoint &#187; PHP</title>
		<atom:link href="http://www.sitepoint.com/blogs/category/php/feed/" rel="self" type="application/rss+xml"/>
		<link>http://www.sitepoint.com/blogs</link>
		<description>News, opinion, and fresh thinking for web developers and designers. The official podcast of sitepoint.com.</description>
		<lastBuildDate>Sat, 07 Nov 2009 08:39:08 +0000</lastBuildDate>
		<generator>http://wordpress.org/?v=2.8.4</generator>
		<language>en</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
		<item>
			<title>Build a Lifestream with SimplePie</title>
			<link>http://www.pheedcontent.com/click.phdo?i=61fffb40d01863049515eef97d10df33</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2009/09/29/build-a-lifestream-with-simplepie/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2009/09/29/build-a-lifestream-with-simplepie/#comments</comments>
			<pubDate>Tue, 29 Sep 2009 09:11:54 +0000</pubDate>
			<dc:creator>raena</dc:creator>
			<category><![CDATA[PHP]]></category>
			<category><![CDATA[Web Tech]]></category>
			<category><![CDATA[rss]]></category>
			<category><![CDATA[simplepie]]></category>
			<category><![CDATA[Tutorials]]></category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=14555</guid>
			<description><![CDATA[If you're anything like Raena, you have bits and pieces of your online life scattered absolutely everywhere — photos, comments, tweets, blogs, favorite links — stashed away in all kinds of profiles. Wouldn’t it be nice to aggregate all those on one page? Find out how in this introduction to the SimplePie feed parser.<br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=61fffb40d01863049515eef97d10df33&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=61fffb40d01863049515eef97d10df33&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><img class=" imgright size-thumbnail wp-image-14559" title="A berry pie" src="http://www.sitepoint.com/blogs/wp-content/uploads/2009/09/290909_red_current_pie-150x150.jpg" alt="A berry pie" width="150" height="150" />If you&#8217;re like me, you have bits and pieces of your online life scattered absolutely everywhere &#8212; photos, comments, tweets, blogs, favorite links &#8212; stashed away in all kinds of profiles. And, of course, many of those profiles offer RSS feeds, ripe for the mashing. Wouldn&#8217;t it be nice to aggregate all those on one page? I think so!</p>
<p>In this tutorial we&#8217;ll build a page that gathers up the RSS feeds of all those little bits of your life and presents them all together in one spot. To do that, we&#8217;ll use SimplePie, a feed parsing class written in PHP. It&#8217;s powerful, it&#8217;s open source, and it&#8217;s easy to use. We&#8217;ll then hack on the output a little, and make it all look gorgeous with HTML and CSS. The techniques we&#8217;ll cover are also useful if you find that you need to aggregate RSS from many sources, like a news page. Let&#8217;s begin!</p>
<h2>What You&#8217;ll Need</h2>
<ul>
<li> The <a href="http://simplepie.org">SimplePie</a> library.</li>
<li> A web server that&#8217;s capable of running it &#8212; there&#8217;s a compatibility test in the SimplePie package that will tell you whether your server is good to go.</li>
<li> If you tweet, <a href="http://www.snipe.net/2009/09/php-twitter-clickable-links/">grab this function by Alison Gianotto</a> to help auto-link those tweets</li>
<li> Of course, you&#8217;ll need your trusty text editor!</li>
<li> For brevity, some of the code is excluded from this tutorial. If you want to see the full picture, <a href="http://sitepoint.com/examples/lifestream/lifestream.phps">view the completed example</a> or <a href="http://sitepoint.com/examples/lifestream/lifestream.phps.zip">download it.</a></li>
</ul>
<div id="adz" class="vertical"></div><h2>Let&#8217;s Begin</h2>
<p>First, of course, we&#8217;ll need to identify the feeds we want to include on the page. Like I said, I have profiles in lots of places, but for now I&#8217;ll keep it short and sweet &#8212; I&#8217;ll grab my blog, <a href="http://twitter.com/">Twitter</a>, <a href="http://flickr.com/">Flickr</a> photos, and favorited tracks from <a href="http://last.fm/">Last.fm.</a></p>
<p>You&#8217;ll need to gather up the URLs for the feeds you want, and stash them someplace safe. To do that, look for RSS icons on the page or in your browser&#8217;s address bar. Paste those URLs in a text file for later reference.</p>
<p>Next, we&#8217;ll need to check that SimplePie is compatible with our hosting environment. There&#8217;s plenty of stuff in this download &#8212; for now, we can just upload the <code>compatibility_test</code> directory in a web-accessible location on the server, and take a look at it in a web browser. Hopefully, there&#8217;ll be a series of messages informing you that a number of dependencies are enabled. If so, it&#8217;s time to upload the rest of the library. (If you have a folder in your download called <code>test</code>, it&#8217;s safe to exclude that one.)</p>
<p>While you&#8217;re poking around in your web server, now&#8217;s a good time to set up a directory for SimplePie to use as a cache. You should make this directory writable. I&#8217;ve chosen <code>cache</code>.</p>
<p>Make a note of where you uploaded SimplePie, and your cache directory. In my case, both are in the same directory as where I plan to put my lifestream page.</p>
<h2>Prepare Your Feeds</h2>
<p>Let&#8217;s grab those feeds! Open up a new text editor and begin a new PHP file, which we&#8217;ll call <code>lifestream.php</code>. This will be the page that eventually shows our lifestream.</p>
<p>First, let&#8217;s include a very important component &#8212; the SimplePie library itself:</p>
<pre><code>&lt;?php
  require_once('./simplepie/simplepie.inc');</code></pre>
<p>Now, we need to define a couple of important variables: the first is to define the cache location, and the second is to specify the amount of time (in seconds) we want to cache the feeds. Hitting feeds repeatedly is rude, and some feed providers will bar you for excessive feed requests. Also, if you have a busy site, it&#8217;s sensible to keep your resource consumption low. I&#8217;ve chosen thirty minutes, or 1800 seconds, which is a nice tradeoff between freshness and politeness. Here are those variables:</p>
<pre><code>$cache = "./cache";
$duration = 1800;</code></pre>
<p>Next, time to create some <code>SimplePie</code> objects. The easiest way to turn a feed into a <code>SimplePie</code> object is to <a href="http://simplepie.org/wiki/reference/simplepie/start">call it in its short form:</a> pass it a feed URL, the location of your cache directory, and the cache duration. All my objects look roughly like this:</p>
<pre><code>$object = new SimplePie(
 "http://example.com/feed.xml",
  $cache, $duration);</code></pre>
<p>You&#8217;ll see I&#8217;m passing those <code>$cache</code> and <code>$duration</code> variables in to save repeating myself. Create objects for each of your feeds &#8212; mine are called <code>$blog</code>, <code>$twitter</code>, <code>$flickr</code>, and <code>$lastfm</code>.</p>
<p>At this stage, since I&#8217;m going to be including tweets, I&#8217;m going to paste in Alison Gianotto&#8217;s <code><a href="http://www.snipe.net/2009/09/php-twitter-clickable-links/">twitterify</a></code><a href="http://www.snipe.net/2009/09/php-twitter-clickable-links/"> function.</a> This function simply accepts some text and looks for items that would be clickable in Twitter &#8212; references to usernames with @, tags with #, and regular URLs &#8212; and wraps them in nice hyperlinks. The function inserts hyperlinks with a <code>target</code> attribute; I&#8217;ve removed those from my example.</p>
<h2>Create a Shell</h2>
<p>It&#8217;s markup time! Create a HTML shell immediately below the closing <code>?&gt;</code> of your PHP script. In this example, I&#8217;ve just shown the contents of the <code>body</code> element for brevity, though of course I&#8217;d include a <code>!DOCTYPE</code>, <code>head</code>, and other necessities:</p>
<pre><code>&lt;h1&gt;My awesome lifestream&lt;/h1&gt;

&lt;div id="blog"&gt;
  &lt;h2&gt;My blog&lt;/h2&gt;
  &lt;ul&gt;

  &lt;/ul&gt;
&lt;/div&gt;
&lt;div id="twitter"&gt;
  &lt;h2&gt;My tweets&lt;/h2&gt;
  &lt;ul&gt;

  &lt;/ul&gt;
&lt;/div&gt;
&lt;div id="flickr"&gt;
  &lt;h2&gt;My pictures&lt;/h2&gt;
  &lt;ul&gt;

  &lt;/ul&gt;
&lt;/div&gt;
&lt;div id="lastfm"&gt;
  &lt;h2&gt;My tunes&lt;/h2&gt;
  &lt;ul&gt;

  &lt;/ul&gt;
&lt;/div&gt;</code></pre>
<h2 id="reveal_the_items">Reveal the Items</h2>
<p>Now that we have a shell of a document, it&#8217;s time to start adding some more PHP to this page to loop through each of the items and echo the relevant parts of each item &#8212; titles, descriptions, enclosures, links, and more &#8212; and wrap the appropriate markup around them. To find out what&#8217;s available, examine the source of your feeds, and take a look at the SimplePie documentation, specifically the stuff that begins with <code>get_</code>. <a href="http://simplepie.org/wiki/reference/simplepie_item/start#g">You&#8217;ll find a list here.</a></p>
<p>Thinking of my own four feeds, each set of items should be slightly different. With my blog posts, I&#8217;ll want to include a title, description, date, and hyperlink. In the Twitter RSS feed, the title and description are identical, so I&#8217;ll just use the title. For the Flickr items, I just want to show the picture. The tracks on Last.fm just need a title, link, and date. So, I&#8217;ll need to create four separate loops.</p>
<p>Here&#8217;s the loop to spit out my blog items, which I&#8217;ve placed inside the unordered list inside <code>div#blog</code>. This one&#8217;s fairly straightforward:</p>
<pre><code>&lt;?php foreach ($blog-&gt;get_items()
  as $item) : ?&gt;
&lt;li&gt;
  &lt;h3&gt;
    &lt;a href="&lt;?php
      echo $item-&gt;get_link(); ?&gt;"&gt;
      &lt;?php echo $item-&gt;get_title(); ?&gt;
    &lt;/a&gt;
  &lt;/h3&gt;
  &lt;p&gt;
    &lt;?php echo $item-&gt;get_description(); ?&gt;
  &lt;/p&gt;
  &lt;p class="date"&gt;
    &lt;a href="&lt;?php
      echo $item-&gt;get_link(); ?&gt;"&gt;
      &lt;?php echo $item-&gt;get_date(); ?&gt;
    &lt;/a&gt;
  &lt;/p&gt;
&lt;/li&gt;
&lt;?php endforeach; ?&gt;</code></pre>
<p>This loop should generate a series of list-items like this, for each blog entry in my feed:</p>
<pre><code>&lt;li&gt;
  &lt;h3&gt;
    &lt;a href="http://example.com/blogentry/"&gt;
      Title
    &lt;/a&gt;
  &lt;/h3&gt;
  &lt;p&gt;Blog entry description text&lt;/p&gt;
  &lt;p class="date"&gt;
    &lt;a href="http://example.com/blogentry/"&gt;
      01 January 2010, 12:00 am
    &lt;/a&gt;
  &lt;/p&gt;
&lt;/li&gt;</code></pre>
<p>Too easy!</p>
<p>When examining my Twitter feed, I&#8217;ve noticed that each tweet is prefixed with my name, a colon, and a space. In this instance, I&#8217;ll want to remove that text. I&#8217;d also like to convert those hashtags, username references and URLs into clickable hyperlinks. In this loop, when I echo the <code>title</code> of the feed item, I&#8217;ll wrap <code>$item-&gt;get_title()</code> in a <code>str_replace</code> statement, and then wrap <em>that</em> in the <code>twitterify</code> function:</p>
<pre><code>&lt;?php foreach ($twitter-&gt;get_items()
  as $item) : ?&gt;
&lt;li&gt;
  &lt;p&gt;
    &lt;?php echo twitterify(
      str_replace(
        "raena: ", "", $item-&gt;get_title()
      )
    ); ?&gt;
  &lt;/p&gt;
  &lt;p class="date"&gt;
    &lt;a href="&lt;?php
        echo $item-&gt;get_link(); ?&gt;"&gt;
      &lt;?php echo $item-&gt;get_date(); ?&gt;
    &lt;/a&gt;
  &lt;/p&gt;
&lt;/li&gt;
&lt;?php endforeach; ?&gt;</code></pre>
<p>Flickr feeds&#8217; descriptions are less than ideal for my purposes: they include the text &#8220;<em>username</em> posted a photo&#8221;, the medium sized image surrounded by a link to the photo page, and the Flickr photo&#8217;s description. However, I&#8217;d just like to present the littlest version of the image and hyperlink, and use the title of the image as the <code>alt</code> text. Fortunately, Flickr provides the URL to the image in the form of an enclosure, so I&#8217;ll use that instead.</p>
<p>Flickr&#8217;s enclosures are still medium-sized, but I want the cute, square thumbnail. That&#8217;s as simple as replacing any reference to <code>_m.jpg</code> with <code>_s.jpg</code>.</p>
<pre><code>&lt;?php foreach ($flickr-&gt;get_items()
  as $item) : ?&gt;
  &lt;li class="item"&gt;
    &lt;a href="&lt;?php echo $item-&gt;get_link(); ?&gt;"&gt;
    &lt;?php foreach ($item-&gt;get_enclosures()
      as $enclosure) : ?&gt;
      &lt;img src="&lt;?php echo str_replace(
        "_m.jpg","_s.jpg",
          $enclosure-&gt;get_link()
      ); ?&gt;"
        alt="&lt;?php
          echo $item-&gt;get_title(); ?&gt;" /&gt;
      &lt;?php endforeach; ?&gt;
    &lt;/a&gt;
  &lt;/li&gt;
&lt;?php endforeach; ?&gt;</code></pre>
<p>Finally, I only need the titles and dates from my list of loved tracks from Last.fm:</p>
<pre><code>&lt;?php foreach ($lastfm-&gt;get_items()
  as $item) : ?&gt;
  &lt;li class="item"&gt;
  &lt;p&gt;
    &lt;a href="&lt;?php
      echo $item-&gt;get_link(); ?&gt;"&gt;
      &lt;?php echo $item-&gt;get_title(); ?&gt;
    &lt;/a&gt;
  &lt;/p&gt;
  &lt;p class="date"&gt;
    &lt;?php echo $item-&gt;get_date(); ?&gt;
  &lt;/p&gt;
   &lt;/li&gt;
 &lt;?php endforeach; ?&gt;</code></pre>
<p>You&#8217;ll find my completed example as <code>lifestream.phps</code> in the code download, or again, <a href="http://sitepoint.com/examples/lifestream/lifestream.phps">view it in full here.</a> If you plan to use it as a basis for your own work, remember to change the placeholder feed URLs to your own!</p>
<p>When you&#8217;re done fine-tuning your items, it&#8217;s time to upload it to the web server, and view it in your browser. If all went well, you should see a series of unordered lists, each containing different goodies from those RSS feeds.</p>
<p>All that&#8217;s left now is to style the page and add any other interesting goodies you feel are necessary. In the example, you&#8217;ll find an extremely simple set of styles: feel free to use them as a basis for your own.</p>
<h2>There&#8217;s More&#8230;</h2>
<p>Of course, there&#8217;s plenty more to SimplePie than the very easy example we&#8217;ve looked at today. Dive into <a href="http://simplepie.org/wiki">the wiki</a> and see what&#8217;s possible &#8212; there&#8217;s heaps of support for enclosure display, the ability to merge multiple feeds into one giant stream, and easy ways to add social media sharing buttons to items. <a href="http://simplepie.org/wiki/ideas/start">You&#8217;ll find some great examples of SimplePie-powered sites on the wiki.</a> Enjoy!</p>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=136&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=61fffb40d01863049515eef97d10df33&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=61fffb40d01863049515eef97d10df33&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2009/09/29/build-a-lifestream-with-simplepie/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		</item>
		<item>
			<title>Free Performance with MySQL Table Types</title>
			<link>http://www.pheedcontent.com/click.phdo?i=6798d6940d37773ff4f0da31bc390106</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2009/09/08/free-performance-with-mysql-table-types/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2009/09/08/free-performance-with-mysql-table-types/#comments</comments>
			<pubDate>Tue, 08 Sep 2009 05:38:01 +0000</pubDate>
			<dc:creator>Kevin Yank</dc:creator>
			<category><![CDATA[PHP]]></category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=14004</guid>
			<description><![CDATA[Last week, SitePoint launched its second Twitaway, giving away a free PDF of The Principles of Successful Freelancing to anyone kind enough to follow us on Twitter. Unfortunately, with the traffic this attracted, our MySQL server ground to a halt in the first few hours of the giveaway. Kevin explains why, and how we fixed it.<br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=6798d6940d37773ff4f0da31bc390106&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=6798d6940d37773ff4f0da31bc390106&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><strong>Last week, SitePoint launched its second <a href="http://twitaway.sitepoint.com/">Twitaway</a>, giving away a free PDF of <a href="http://www.sitepoint.com/books/freelancer1/"><em>The Principles of Successful Freelancing</em></a> to anyone kind enough to <a href="http://twitter.com/sitepointdotcom/">follow us on Twitter</a>. Predictably, the site we built for readers to claim the PDF was swamped with traffic in the first few hours of the giveaway, and the MySQL server behind the scenes quickly ground to a halt.</strong></p>
<p>First, a little background: at the time of our first Twitaway in 2008, Twitter HQ had been kind enough to relax the limit it places on the maximum number of Twitter direct messages (DMs) that it would allow us to send in a single day. To distribute the free PDFs, then, we simply wrote a PHP script that would look through our Twitter followers every few minutes, identify the new ones and send them a DM with the download link.</p>
<p>This time around, when we sat down to plan Twitaway 2009, it was clear our job would be more difficult. Much less cooperative than they had been previously, Twitter HQ stonewalled our requests to relax the daily DM limit—they wouldn’t even tell us what our daily limit <em>was</em>. For awhile there, we weren’t sure how we would be able to deliver a download link privately to our Twitter followers.</p>
<p>The solution finally came from Mal, a new developer on the team here at SitePoint HQ. We would use Twitter’s new <a href="http://apiwiki.twitter.com/OAuth-FAQ">OAuth support</a> to request temporary access to your Twitter account, use it to verify that you were indeed following us, and then simply display the PDF download link in your browser. No DM required!</p>
<p>Of course, writing a PHP application that supported this OAuth-based workflow was a lot more complicated than writing a static web page with a “follow us on Twitter” link. Not only would it be harder to write, but it would be harder to handle large amounts of traffic hitting it, too.</p>
<p>So there we were: just past midnight on launch day of the Twitaway, and the application had stopped responding. Our monitoring systems made it clear that, while our PHP servers were weathering the storm nicely, the MySQL database server had spiked to 100% CPU usage, stuck its fingers in its ears and begun rocking back and forth, chanting “na-na-na-na-na-I’m-not-listening!”</p>
<div id="adz" class="vertical"></div><p>As it turned out, this was an important lesson for our development team about the importance of choosing appropriate MySQL table types. Read on for how we solved the problem, and why we should have predicted it.</p>
<h2>Choosing The Right Table Type</h2>
<p>When we logged into the swamped MySQL server, we could see (using the <a href="http://dev.mysql.com/doc/refman/5.4/en/show-processlist.html">SHOW PROCESSLIST</a> command) that the overwhelming majority of the queries that it was receiving were for the database table we had set up to contain PHP session data.</p>
<p>By default, PHP stores your users’ session variables in files on the web server’s hard drive. In our case, however, we had multiple web servers that all needed to share the same session data, so we had configured PHP to store its sessions on the MySQL database server instead.</p>
<p>Unfortunately, we had failed to specify a table type when creating the session table in the database:</p>
<pre><code class="sql">CREATE TABLE sessions (
  sesskey VARCHAR(32) NOT NULL DEFAULT '' PRIMARY KEY,
  expiry INT(11),
  expireref VARCHAR(64),
  sessdata LONGTEXT
);</code></pre>
<p>MySQL supports a number of different <a href="http://dev.mysql.com/doc/refman/5.4/en/storage-engines.html">storage engines</a>. When you create a table, by default MySQL will use the <a href="http://dev.mysql.com/doc/refman/5.4/en/myisam-storage-engine.html"><strong>MyISAM storage engine</strong></a> to create a MyISAM table. MyISAM is a relatively simple table type, which forgoes several advanced features in order to offer improved performance. In the early days of MySQL, the MyISAM table type was instrumental in making it one of the best performing databases around.</p>
<p>These characteristics would seem to make MyISAM a natural choice for something like a session data table. Unfortunately, MyISAM has an achilles heel: <strong>table-level locking</strong>. In order to write a change to a MyISAM table, MySQL must lock the whole table, preventing any other query from reading the table until the change is complete.</p>
<p>As thousands upon thousands of SitePoint readers flocked to our Twitaway site to claim their book, multiple PHP processes competed to create, update, and read session data in this single table. And because of MyISAM’s table-level locking, they had to do so one at a time, waiting their turn on an increasingly backed-up database server.</p>
<p>On the surface, the <a href="http://dev.mysql.com/doc/refman/5.4/en/innodb.html"><strong>InnoDB storage engine</strong></a> would seem to be a poor choice for improving database performance. It supports a bunch of features that MyISAM doesn’t, including <a href="http://dev.mysql.com/doc/refman/5.4/en/storage-engine-compare-transactions.html">transactions</a> and <a href="http://dev.mysql.com/doc/refman/5.4/en/innodb-foreign-key-constraints.html">enforced referential integrity</a>, and all the literature will tell you that writing data to an InnoDB table is slower than with a MyISAM table.</p>
<p>But where MyISAM has an achilles heel, InnoDB has a secret weapon: <strong>row-level locking</strong>. When writing data to an InnoDB table, only the affected rows are locked, not the whole table. Queries accessing other parts of the same table can happen simultaneously. In a situation like session data, where each browser request being processed is likely to relate to a different session, row-level locking is just what the doctor ordered!</p>
<p>With this in mind, you can probably guess how we cured our swamped database server. We simply converted the session table to an InnoDB table:</p>
<pre><code class="sql">ALTER TABLE sessions ENGINE = InnoDB;</code></pre>
<p>Instantly, the CPU load on the database server dropped to more reasonable levels, the Twitaway site came back online, and there was much rejoicing. We could have saved ourselves a lot of grief simply by specifying the table type when we first created the table:</p>
<pre><code class="sql">CREATE TABLE sessions (
  sesskey VARCHAR(32) NOT NULL DEFAULT '' PRIMARY KEY,
  expiry INT(11),
  expireref VARCHAR(64),
  sessdata LONGTEXT
) ENGINE = InnoDB;</code></pre>
<p>Switching to InnoDB got us through that first night, but by the next morning we were wondering if there might be an even better table type for us to use. After a little research, the <a href="http://dev.mysql.com/doc/refman/5.4/en/memory-storage-engine.html"><strong>Memory storage engine</strong></a> looked like a likely candidate.</p>
<p>If MyISAM sacrifices features for performance, the Memory table type takes this to extremes. Memory tables are never written to disk, so if you restart your database server, the data stored in any Memory tables is lost. The fact that Memory tables require no disk access, however, makes them blazing fast! Since session data is intended to be volatile anyway, we didn’t really mind if this particular table wouldn’t survive a server restart.</p>
<p>According to the documentation, Memory tables have the same table-level locking weakness as MyISAM. Nevertheless, when we switched our session table to the Memory storage engine, our database server’s CPU usage dropped to virtually zero!</p>
<p>Memory tables have some harsh restrictions that mean they aren’t a cure-all for performance issues. Disappearing data aside, Memory tables cannot contain large column types like TEXT or BLOB. Indeed, the largest piece of data you can store in a Memory table row is a 255 character CHAR or VARCHAR. In our case, we actually had to shorten a couple of our session variable names to make sure they would fit!</p>
<p>At this point, our session table is gliding along happily under moderate load. As the traffic from this article and some other promotion we have planned hits the server, we will be watching closely to decide if the Memory storage engine is up to the task, or if we should switch back to InnoDB to take advantage of row-level locking. In either case, we learned an important lesson: <strong>when performance counts, take the time to think about you choice of MySQL storage engine.</strong> In our case, it took us from a swamped database server to one showing zero CPU usage.</p>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=136&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=6798d6940d37773ff4f0da31bc390106&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=6798d6940d37773ff4f0da31bc390106&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2009/09/08/free-performance-with-mysql-table-types/feed/</wfw:commentRss>
			<slash:comments>22</slash:comments>
		</item>
		<item>
			<title>Are PHP Namespaces Really So Bad?</title>
			<link>http://www.pheedcontent.com/click.phdo?i=372966f3b686f9bf33bcd13a89aba1b5</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2009/08/13/are-php-namespaces-bad/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2009/08/13/are-php-namespaces-bad/#comments</comments>
			<pubDate>Wed, 12 Aug 2009 16:52:18 +0000</pubDate>
			<dc:creator>Craig Buckler</dc:creator>
			<category><![CDATA[PHP]]></category>
			<category>namespaces</category>
			<category>php</category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=12356</guid>
			<description><![CDATA[Namespaces have caused a divide amongst developers who either love or hate their implementation in PHP 5.3. Craig looks at the issues and assures us there is little need to worry.<br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=372966f3b686f9bf33bcd13a89aba1b5&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=372966f3b686f9bf33bcd13a89aba1b5&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.sitepoint.com/blogs/wp-content/uploads/2009/07/php-namespaces1.png" alt="PHP namespaces" title="PHP namespaces" width="240" height="240" class="imgright" />PHP developers have been <a href="http://www.petitiononline.com/phpns/petition.html">demanding namespaces</a> for some time. As PHP applications have grown larger and more complex, namespaces have become essential to prevent code clashes.</p>
<p>My <a href="http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/">recent tutorials</a> received a number of comments complaining about namespace implementation in PHP. The main issues were the syntax and the backslash character. Before I tackle those issues, let&#8217;s take a quick look back at the history of PHP.</p>
<h2>PHP is a Mess</h2>
<p>Languages such as C# and Java were designed and follow rigorous syntax standards. PHP has <em>evolved</em>. The original version was released in 1995 and, by version 3, it was a popular procedural programming language. Version 4 introduced rudimentary object orientation and version 5 provides a reasonably standard OOP model. Namespaces have now been added to version 5.3.</p>
<div id="adz" class="vertical"></div><p>PHP critics will argue that the language is a mess. Function names are inconsistent (e.g. strpos, str_split, substr), object handling has been tagged on, and some of the syntax is different &#8212; if not bizarre &#8212; when compared with other languages.</p>
<p>However, PHP remains the most widely-used server-side development language. Its versatility is one of its primary strengths:</p>
<ul>
<li>Novice developers can start with simple procedural programming. They need never touch OOP techniques and can still be productive.</li>
<li>Code written 10 years ago in PHP 3 still works today in PHP 5.3. A few minor tweaks may be required, but major rewrites are rarely necessary.</li>
</ul>
<p>PHP code may not always be pretty, logical, or elegant, but development is rapid and often easier to comprehend than the alternatives.</p>
<h2>PHP Namespace Implementation</h2>
<p>Unlike C# and Java, PHP has to retain compatibility with non-namespaced code. That has been achieved and you can choose whether to use namespaces or not. However, if you&#8217;re using PHP 5.3 (or above), I would recommend them &#8212; even if you simply use the same name throughout the whole of your project.</p>
<p>The choice of <code>namespace</code> and <code>use</code> as namespace operators seems logical. Some developers may disagree, but that would have been the case no matter what they&#8217;d been named. </p>
<p>Finally, we come to the backslash character. Most critics complain that it&#8217;s ugly, difficult to read, and awkward to type on a Mac. However, I still consider it preferable to the double-colon that was originally proposed. Examine the following static method call:</p>
<pre><code class="php">
// PHP 5.3 beta static method call
echo ::App::Lib1::MyClass::WhoAmI();

// PHP 5.3 final static method call
echo \App\Lib1\MyClass::WhoAmI();
</code></pre>
<p>The second line is quicker to type, less error-prone, easier to read, and simpler to understand. If you see a backslash outside of a string, you know namespacing must be involved.</p>
<p>Of course, it would be great if PHP used a &#8216;.&#8217; period for public methods, static methods, and namespaces. That would make it consistent with Java, C#, JavaScript, Python and many other languages. Unfortunately, PHP&#8217;s history and backwards compatibility makes that difficult to achieve.</p>
<p>No language is perfect, and PHP is far from it! However, namespacing has been implemented well, especially when you consider the restrictions and problems it could have caused. I&#8217;m sure you&#8217;ll learn to love that backslash!</p>
<p>Related reading:</p>
<ul>
<li><a href="http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/">How to Use PHP Namespaces, Part 1: The Basics</a></li>
<li><a href="http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/">How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution</a></li>
<li><a href="http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/">How to Use PHP Namespaces, Part 3: Keywords and Autoloading</a></li>
</ul>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=136&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=372966f3b686f9bf33bcd13a89aba1b5&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=372966f3b686f9bf33bcd13a89aba1b5&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2009/08/13/are-php-namespaces-bad/feed/</wfw:commentRss>
			<slash:comments>23</slash:comments>
		</item>
		<item>
			<title>How to Use PHP Namespaces, Part 3: Keywords and Autoloading</title>
			<link>http://www.pheedcontent.com/click.phdo?i=4f625e60e98f456998e327cde7f111a5</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/#comments</comments>
			<pubDate>Wed, 15 Jul 2009 08:56:07 +0000</pubDate>
			<dc:creator>Craig Buckler</dc:creator>
			<category><![CDATA[PHP]]></category>
			<category>namespaces</category>
			<category>php</category>
			<category>php5.3</category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=12194</guid>
			<description><![CDATA[In the final part of his series explaining PHP namespaces, Craig covers more advanced topics such as the keywords and class autoloading.<br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=4f625e60e98f456998e327cde7f111a5&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=4f625e60e98f456998e327cde7f111a5&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.sitepoint.com/blogs/wp-content/uploads/2009/07/php-namespaces1.png" alt="PHP namespaces" title="PHP namespaces" width="240" height="240" class="imgright" />In parts 1 and 2 of this series, we looked at PHP <a href="http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics">namespace basics</a>, the <a href="http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/">use operator, and name resolution</a>. In this final article we discuss some of the more advanced namespace options.</p>
<h2>The __NAMESPACE__ Constant</h2>
<p>__NAMESPACE__ is a PHP string that always returns the current namespace name. In the global space it will be an empty string.</p>
<pre><code class="php">
&lt;?php
namespace App\Lib1;
echo __NAMESPACE__; // outputs: App\Lib1
?&gt;
</code></pre>
<p>The value has obvious benefits during debugging. It can also be used to dynamically generate a fully-qualified class name, e.g.</p>
<div id="adz" class="horizontal"></div><pre><code class="php">
&lt;?php
namespace App\Lib1;

class MyClass {
	public function WhoAmI() {
		return __METHOD__;
	}
}

$c = __NAMESPACE__ . '\\MyClass';
$m = new $c;
echo $m->WhoAmI(); // outputs: App\Lib1\MyClass::WhoAmI
?&gt;
</code></pre>
<h2>The namespace Keyword</h2>
<p>The <code>namespace</code> keyword can be used to explicitly reference an item within the current namespace or a sub-namespace. It is the namespace equivalent of <code>self</code> within classes:</p>
<pre><code class="php">
&lt;?php
namespace App\Lib1;

class MyClass {
	public function WhoAmI() {
		return __METHOD__;
	}
}

$m = new namespace\MyClass;
echo $m->WhoAmI(); // outputs: App\Lib1\MyClass::WhoAmI
?&gt;
</code></pre>
<h2>Autoloading Namespaced Classes</h2>
<p>One of the best time-saving features of PHP 5 is autoloading. In global (non-namespaced) PHP code, a standard autoload function could be written:</p>
<pre><code class="php">
&lt;?php
$obj1 = new MyClass1(); // classes/MyClass1.php is auto-loaded
$obj2 = new MyClass2(); // classes/MyClass2.php is auto-loaded

// autoload function
function __autoload($class_name) {
	require_once("classes/$class_name.php");
}
?&gt;
</code></pre>
<p>In PHP 5.3, you can create an instance of a namespaced class. In that situation, the fully-qualified namespace and class name is passed to the __autoload function, e.g. the value of $class_name could be &#8220;App\Lib1\MyClass&#8221;. You could continue to place all your PHP class files in the same folder and strip the namespace from the string, however, that could result in file name clashes.</p>
<p>Alternatively, your class file hierarchy could be organized in the same way as your namespace structure. For example, a MyClass.php file could be created in the folder /classes/App/Lib1:</p>
<p><strong>/classes/App/Lib1/MyClass.php:</strong></p>
<pre><code class="php">
&lt;?php
namespace App\Lib1;

class MyClass {
	public function WhoAmI() {
		return __METHOD__;
	}
}
?&gt;
</code></pre>
<p>A file in root folder could then use the following code:</p>
<p><strong>myapp.php:</strong></p>
<pre><code class="php">
&lt;?php
use App\Lib1\MyClass as MC;

$obj = new MC();
echo $obj->WhoAmI();

// autoload function
function __autoload($class) {

	// convert namespace to full file path
	$class = 'classes/' . str_replace('\\', '/', $class) . '.php';
	require_once($class);

}
?&gt;
</code></pre>
<p>Explanation:</p>
<ol>
<li>The class App\Lib1\MyClass is aliased as MC.</li>
<li><code>new MC()</code> is translated to <code>new App\Lib1\MyClass()</code> during compilation.</li>
<li>The string &#8220;App\Lib1\MyClass&#8221; is passed to the __autoload function. This replaces all namespace backslashes with file path forward slashes, and modifies the string so the file &#8220;classes\App\Lib1\MyClass.php&#8221; is loaded.</li>
</ol>
<p>I hope you found this series of PHP namespace articles useful. Will you be namespacing in your PHP code?</p>
<p>See also:</p>
<ul>
<li><a href="http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics">How to Use PHP Namespaces, Part 1: The Basics</a></li>
<li><a href="http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/">How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution</a></li>
<li><a href="http://www.sitepoint.com/blogs/2009/07/07/install-php53-windows/">How to Install PHP 5.3 on Windows</a></li>
</ul>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=137&amp;did=adz&amp;adtype=horizontal" type="text/javascript"></script><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=4f625e60e98f456998e327cde7f111a5&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=4f625e60e98f456998e327cde7f111a5&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/feed/</wfw:commentRss>
			<slash:comments>10</slash:comments>
		</item>
		<item>
			<title>How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution</title>
			<link>http://www.pheedcontent.com/click.phdo?i=a2cb6fb629a7af1f1d9884d913e98492</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/#comments</comments>
			<pubDate>Tue, 14 Jul 2009 08:00:15 +0000</pubDate>
			<dc:creator>Craig Buckler</dc:creator>
			<category><![CDATA[PHP]]></category>
			<category>namespaces</category>
			<category>php</category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=12070</guid>
			<description><![CDATA[In the second part of Craig's PHP namespaces series, he looks at importing, aliasing, and identifier resolution.<br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=a2cb6fb629a7af1f1d9884d913e98492&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=a2cb6fb629a7af1f1d9884d913e98492&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.sitepoint.com/blogs/wp-content/uploads/2009/07/php-namespaces1.png" alt="PHP namespaces" title="PHP namespaces" width="240" height="240" class="imgright" />In <a href="http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics">part 1</a>, we discussed why PHP namespaces are useful and the <code>namespace</code> keyword. In this article, we examine the <code>use</code> command and the way PHP resolves namespace names.</p>
<p>For the purposes of this example, we will define two almost identical code blocks; the only difference is their namespace:</p>
<p style="clear:right;"><strong>lib1.php:</strong></p>
<pre><code class="php">
&lt;?php
// application library 1
namespace App\Lib1;

const MYCONST = 'App\Lib1\MYCONST';

function MyFunction() {
	return __FUNCTION__;
}

class MyClass {
	static function WhoAmI() {
		return __METHOD__;
	}
}
?&gt;
</code></pre>
<p><strong>lib2.php:</strong></p>
<pre><code class="php">
&lt;?php
// application library 2
namespace App\Lib2;

const MYCONST = 'App\Lib2\MYCONST';

function MyFunction() {
	return __FUNCTION__;
}

class MyClass {
	static function WhoAmI() {
		return __METHOD__;
	}
}
?&gt;
</code></pre>
<p>There is a little PHP terminology to understand before we begin&#8230;</p>
<div id="adz" class="vertical"></div><p><strong>Fully-qualified name</strong><br />
Any PHP code can refer to a fully-qualified name &#8212; an identifier starting with the namespace backslash separator, e.g. \App\Lib1\MYCONST, \App\Lib2\MyFunction(), etc. </p>
<p>Fully-qualified names have no ambiguity. The initial backslash operates in a similar way to a file path; it signifies the <em>&#8216;root&#8217;</em> global space. If we implemented a different MyFunction() in our global space, it could be called from lib1.php or lib2.php using <code>\MyFunction()</code>.</p>
<p>Fully-qualified names are useful for one-off function calls or object initialization. However, they can become impractical when you are making lots of calls. As we will discover below, PHP offers other options to save us from namespace typing cramps.</p>
<p><strong>Qualified name</strong><br />
An identifier with at least one namespace separator, e.g. Lib1\MyFunction().</p>
<p><strong>Unqualified name</strong><br />
An identifier without a namespace separator, e.g. MyFunction().</p>
<h2>Working Within the Same Namespace</h2>
<p>Consider the following code:</p>
<p><strong>myapp1.php:</strong></p>
<pre><code class="php">
&lt;?php
namespace App\Lib1;

require_once('lib1.php');
require_once('lib2.php');

header('Content-type: text/plain');
echo MYCONST . "\n";
echo MyFunction() . "\n";
echo MyClass::WhoAmI() . "\n";
?&gt;
</code></pre>
<p>Although we include both <strong>lib1.php</strong> and <strong>lib2.php</strong>, the identifiers MYCONST, MyFunction, and MyClass will only reference code in <strong>lib1.php</strong>. This occurs because the <strong>myapp1.php</strong> code is within the same App\Lib1 namespace:</p>
<p><em>result:</em></p>
<pre><code class="text">
App\Lib1\MYCONST
App\Lib1\MyFunction
App\Lib1\MyClass::WhoAmI
</code></pre>
<h2>Namespace Importing</h2>
<p>Namespaces can be imported with the <code>use</code> operator, e.g.</p>
<p><strong>myapp2.php:</strong></p>
<pre><code class="php">
&lt;?php
use App\Lib2;

require_once('lib1.php');
require_once('lib2.php');

header('Content-type: text/plain');
echo Lib2\MYCONST . "\n";
echo Lib2\MyFunction() . "\n";
echo Lib2\MyClass::WhoAmI() . "\n";
?&gt;
</code></pre>
<p>Any number of <code>use</code> statements can be defined or you can separate individual namespaces with a comma. In this example we have imported the App\Lib2 namespace. We still cannot refer directly to MYCONST, MyFunction or MyClass because our code is in the global space and PHP will look for them there. However, if we add a prefix of &#8216;Lib2\&#8217;, they become qualified names; PHP will search through the imported namespaces until it finds a match.</p>
<p><em>result:</em></p>
<pre><code class="text">
App\Lib2\MYCONST
App\Lib2\MyFunction
App\Lib2\MyClass::WhoAmI
</code></pre>
<h2>Namespace Aliases</h2>
<p>Namespace aliases are perhaps the most useful construct. Aliases allow us to reference long namespaces using a shorter name.</p>
<p><strong>myapp3.php:</strong></p>
<pre><code class="php">
&lt;?php
use App\Lib1 as L;
use App\Lib2\MyClass as Obj;

header('Content-type: text/plain');
require_once('lib1.php');
require_once('lib2.php');

echo L\MYCONST . "\n";
echo L\MyFunction() . "\n";
echo L\MyClass::WhoAmI() . "\n";
echo Obj::WhoAmI() . "\n";
?&gt;
</code></pre>
<p>The first <code>use</code> statement defines App\Lib1 as &#8216;L&#8217;. Any qualified names using &#8216;L&#8217; will be translated to &#8216;App\Lib1&#8242; at compile-time. We can therefore refer to L\MYCONST and L\MyFunction rather than the fully-qualified name.</p>
<p>The second <code>use</code> statement is more interesting. It defines &#8216;Obj&#8217; as an alias for the <strong>class</strong> &#8216;MyClass&#8217; within the App\Lib2\ namespace. This is only possible for classes &#8212; not constants or functions. We can now use <code>new Obj()</code> or run static methods as shown above.</p>
<p><em>result:</em></p>
<pre><code class="text">
App\Lib1\MYCONST
App\Lib1\MyFunction
App\Lib1\MyClass::WhoAmI
App\Lib2\MyClass::WhoAmI
</code></pre>
<h2>PHP Name Resolution Rules</h2>
<p>PHP identifier names are resolved using the following namespace rules. Refer to the <a href="http://www.php.net/manual/pl/language.namespaces.rules.php">PHP manual</a> for more information.</p>
<p>1. Calls to fully-qualified functions, classes or constants are resolved at compile-time.</p>
<p>2. Unqualified and qualified names are translated according to the import rules, e.g. if the namespace A\B\C is imported as C, a call to <code>C\D\e()</code> is translated to <code>A\B\C\D\e()</code>.</p>
<p>3. Inside a namespace, all qualified names not already translated according to import rules have the current namespace prepended, e.g. if a call to <code>C\D\e()</code> is performed within namespace A\B, it is translated to <code>A\B\C\D\e()</code>.</p>
<p>4. Unqualified class names are translated according to current import rules and the full name is substituted for short imported name, e.g. if class C in namespace A\B is imported as X, <code>new X()</code> is translated to <code>new A\B\C()</code>.</p>
<p>5. Unqualified function calls within a namespace are resolved at run-time. For example, if MyFunction() is called within namespace A\B, PHP first looks for the function \A\B\MyFunction(). If that is not found, it looks for \MyFunction() in the global space.</p>
<p>6. Calls to unqualified or qualified class names are resolved at run-time. For example, if we call <code>new C()</code> within namespace A\B, PHP will look for the class A\B\C. If that is not found, it will attempt to autoload A\B\C.</p>
<p>See also:</p>
<ul>
<li><a href="http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics">How to Use PHP Namespaces, Part 1: The Basics</a></li>
<li><a href="http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/">How to Use PHP Namespaces, Part 3: Keywords and Autoloading</a></li>
<li><a href="http://www.sitepoint.com/blogs/2009/07/07/install-php53-windows/">How to Install PHP 5.3 on Windows</a></li>
</ul>
<p>In tomorrow&#8217;s final article we will cover autoloading and other advanced options.</p>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=136&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=a2cb6fb629a7af1f1d9884d913e98492&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=a2cb6fb629a7af1f1d9884d913e98492&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/feed/</wfw:commentRss>
			<slash:comments>15</slash:comments>
		</item>
		<item>
			<title>How to Use PHP Namespaces, Part 1: The Basics</title>
			<link>http://www.pheedcontent.com/click.phdo?i=488f0362821ca188101d1436f1751ca0</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/#comments</comments>
			<pubDate>Mon, 13 Jul 2009 06:49:04 +0000</pubDate>
			<dc:creator>Craig Buckler</dc:creator>
			<category><![CDATA[PHP]]></category>
			<category>namespaces</category>
			<category>php</category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=12000</guid>
			<description><![CDATA[In the first part of a series of articles, Craig explains why namespaces were required in PHP and how they can be used in your code.<br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=488f0362821ca188101d1436f1751ca0&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=488f0362821ca188101d1436f1751ca0&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.sitepoint.com/blogs/wp-content/uploads/2009/07/php-namespaces.png" alt="PHP 5.3 namespaces" title="PHP 5.3 namespaces" width="240" height="240" class="imgright" />Namespaces are one of the most significant changes in PHP 5.3. They will be familiar to C# and Java developers, and they are likely to change the structure of PHP applications for the better.</p>
<h2>Why Do We Need Namespaces?</h2>
<p>As the size of your PHP code library increases, there is increased risk of accidentally re-defining a function or class name that has been declared before. The problem is exacerbated when you attempt to add third-party components or plugins; what if two or more code sets implement a &#8216;Database&#8217; or &#8216;User&#8217; class?</p>
<p>Until now, the only solution has been long class/function names. For example, WordPress prefixes every name with &#8216;WP_&#8217;. The Zend Framework uses a highly descriptive naming convention that results in long-winded class names such as <code>Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive</code>.</p>
<div id="adz" class="horizontal"></div><p>Name collision problems can be solved with namespaces. PHP constants, classes, and functions can be grouped into namespaced libraries.</p>
<h2>How are Namespaces Defined?</h2>
<p>By default, all constant, class, and function names are placed in the global space &#8212; like they were before PHP supported namespaces.</p>
<p>Namespaced code is defined using a single <code>namespace</code> keyword at the top of your PHP file. It <strong>must</strong> be the first command (with the exception of <code>declare</code>) and no non-PHP code, HTML, or white-space can precede the command, e.g.</p>
<pre><code class="php">
&lt;?php
// define this code in the 'MyProject' namespace
namespace MyProject;

// ... code ...
</code></pre>
<p>The code following this line will be assigned to the &#8216;MyProject&#8217; namespace. It is not possible to nest namespaces or define more than one namespace for the same code block (only the last will be recognized). However, you can define different namespaced code in the same file, e.g.</p>
<pre><code class="php">
&lt;?php
namespace MyProject1;
// PHP code for the MyProject1 namespace

namespace MyProject2;
// PHP code for the MyProject2 namespace

// Alternative syntax
namespace MyProject3 {
	// PHP code for the MyProject3 namespace
}
?&gt;
</code></pre>
<p>Although this is possible, I would advise against it: retain your sanity by defining a single namespace per file.</p>
<p><strong>Sub-namespaces</strong><br />
PHP allows you to define a hierarchy of namespaces so libraries can be sub-divided. Sub-namespaces are separated using a backslash (\) character, e.g.</p>
<ul>
<li>MyProject\SubName</li>
<li>MyProject\Database\MySQL</li>
<li>CompanyName\MyProject\Library\Common\Widget1</li>
</ul>
<h2>Calling Namespaced Code</h2>
<p>In a file named lib1.php, we will define a constant, a function, and a class within the App\Lib1 namespace:</p>
<p><strong>lib1.php</strong></p>
<pre><code class="php">
&lt;?php
// application library 1
namespace App\Lib1;

const MYCONST = 'App\Lib1\MYCONST';

function MyFunction() {
	return __FUNCTION__;
}

class MyClass {
	static function WhoAmI() {
		return __METHOD__;
	}
}
?&gt;
</code></pre>
<p>We can now include this code in another PHP file, e.g.</p>
<p><strong>myapp.php</strong></p>
<pre><code class="php">
&lt;?php
header('Content-type: text/plain');
require_once('lib1.php');

echo \App\Lib1\MYCONST . "\n";
echo \App\Lib1\MyFunction() . "\n";
echo \App\Lib1\MyClass::WhoAmI() . "\n";
?&gt;
</code></pre>
<p>No namespace is defined in myapp.php so the code exists in the global space. Any direct reference to MYCONST, MyFunction or MyClass will fail because they exist in the App\Lib1 namespace. To call code in lib1.php, we can add a prefix of \App\Lib1 to define fully-qualified names. The following result is output when we load myapp.php:</p>
<pre><code class="text">
App\Lib1\MYCONST
App\Lib1\MyFunction
App\Lib1\MyClass::WhoAmI
</code></pre>
<p>Fully-qualified names can become quite long and there are few obvious benefits over defining long class names such as App-Lib1-MyClass. Therefore, in the next article, we will discuss aliasing and take a closer look at how PHP resolves namespace names.</p>
<p>See also:</p>
<ul>
<li><a href="http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/">How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution</a></li>
<li><a href="http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/">How to Use PHP Namespaces, Part 3: Keywords and Autoloading</a></li>
<li><a href="http://www.sitepoint.com/blogs/2009/07/07/install-php53-windows/">How to Install PHP 5.3 on Windows</a></li>
</ul>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=137&amp;did=adz&amp;adtype=horizontal" type="text/javascript"></script><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=488f0362821ca188101d1436f1751ca0&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=488f0362821ca188101d1436f1751ca0&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/feed/</wfw:commentRss>
			<slash:comments>56</slash:comments>
		</item>
		<item>
			<title>Pretty URLs: Pretty Easy!</title>
			<link>http://www.pheedcontent.com/click.phdo?i=86cd376c94d9f9237022cfe006848d4f</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2009/07/07/pretty-urls-pretty-easy/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2009/07/07/pretty-urls-pretty-easy/#comments</comments>
			<pubDate>Tue, 07 Jul 2009 08:33:39 +0000</pubDate>
			<dc:creator>Kevin Yank</dc:creator>
			<category><![CDATA[PHP]]></category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=12058</guid>
			<description><![CDATA[The fourth edition of Build Your Own Database Driven Website Using PHP &#038; MySQL is so much more than a little update -- it approaches development in a whole new way. Here, Kevin explains the book's new method for creating pretty URLs with little effort.<br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=86cd376c94d9f9237022cfe006848d4f&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=86cd376c94d9f9237022cfe006848d4f&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p>
  <strong>Back when I wrote the third edition of <a href="http://www.sitepoint.com/books/phpmysql4/"><em>Build Your Own Database Driven Web Site Using PHP &amp; MySQL</em></a>, it was still acceptable (though certainly unfashionable) to lump all of the code together to generate a web page into a single file, and then use a URL naming that file to access the page.</strong>
</p>
<p>
  PHP has moved on. These days, a developer that builds web sites that way is unlikely to gain a job (except perhaps one that pays poorly). Developers are expected to organize their code into multiple files, and URLs containing file names like index.php are definitely on their way out.
</p>
<p>
  Experienced developers will use finely-crafted frameworks and web server configurations to convert so-called <em>pretty</em> URLs like http://www.example.com/thingy/. No &quot;.php&quot; advertising the fact that the site is built in PHP, no specific filename at all. URLs like this one are designed to be as simple and memorable as possible.
</p>
<p>
  For the fourth edition of the book, I wanted developers to make a habit of this earlier, without forcing them to fiddle with their server configuration, or learn a complex PHP framework.
</p>
<div id="adz" class="vertical"></div><p>
  The easiest possible way I could find to produce pretty URLs was to take advantage of a feature built into every PHP-equipped web server out there: a URL that points to a directory on the server will automatically run a PHP script named <code>index.php</code> sitting in that directory.
</p>
<p>
  With that in mind, here&#8217;s how all of the examples in the book from Chapter 3 onward are structured:
</p>
<ul>
<li>
    <strong>A folder</strong><br />Everything goes inside a folder, whose name is the only visible part in the URL. So if the example displays &quot;thingies&quot;, you would start by creating a folder named thingies, and the URL of the example would be http://www.example.com/thingies/
  </li>
<li>
    <strong>A controller, index.php</strong><br />When a browser requests the example&#8217;s URL, the server will automatically run the index.php script contained in the folder. This file is called the <strong>controller</strong>, and contains all the logic for handling browser requests and performing tasks in response.
  </li>
<li>
    <strong>Include files, like db.inc.php</strong><br />Pieces of code that will be useful in more than one example are pulled out of index.php and placed in separate PHP files. This is basic stuff for PHP veterans, but it&#8217;s a valuable coding technique that was covered much later in the third edition, in Chapter 12. Now it&#8217;s in Chapter 3.
  </li>
<li>
    <strong>Template files, like thingy.html.php</strong><br />Once the controller (with the assistance of one or more include files) does all the work, it must prepare a web page to send back to the browser in response. Rather than slopping all the HTML code into index.php (which is what we did all the way through the third edition of the book), the fourth edition uses a separate file for the HTML code for each type of page. These template files consist mostly of pure HTML; the only PHP code they contain is there to output the values of PHP variables created by the controller. The code in these files is kept very simple, so that they can be handed off to designers who may be unfamiliar with PHP at all. Also, using separate files makes it easy to generate several different types of page (that is, serveral different template files) from a single controller.
  </li>
</ul>
<p>
  All the little changes in approach like this one are what really makes me proud of <a href="http://www.sitepoint.com/books/phpmysql4/"><em>Build Your Own Database Driven Web Site Using PHP &amp; MySQL, 4th Edition</em></a>. I used to describe the third edition of this book as giving beginners &quot;just enough rope to hang themselves with.&quot;
</p>
<p>
  With the fourth edition, beginners can breeze past the self-strangulation stage &#8212; hopefully straight into a well-paying career as a PHP developer!
</p>
<p>I bet you&#8217;re wondering where the code is, right? To see how the &#8220;pretty URL&#8221; technique described above looks in code, be sure to check out Chapter 3 when we publish it on Thursday. Or if you&#8217;re unable to wait, <a href="http://www.sitepoint.com/books/phpmysql4/">grab all four chapters in PDF format today</a> &#8212; free of charge.</p>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=136&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=86cd376c94d9f9237022cfe006848d4f&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=86cd376c94d9f9237022cfe006848d4f&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2009/07/07/pretty-urls-pretty-easy/feed/</wfw:commentRss>
			<slash:comments>26</slash:comments>
		</item>
		<item>
			<title>How to Install PHP 5.3 on Windows</title>
			<link>http://www.pheedcontent.com/click.phdo?i=884dee5c0ed4731ee41779430c953841</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2009/07/07/install-php53-windows/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2009/07/07/install-php53-windows/#comments</comments>
			<pubDate>Tue, 07 Jul 2009 07:22:17 +0000</pubDate>
			<dc:creator>Craig Buckler</dc:creator>
			<category><![CDATA[PHP]]></category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=11932</guid>
			<description><![CDATA[PHP 5.3 is the most significant update since version 5.0. Craig provides installation instructions for your Windows development PC and points out a number of problems that you may encounter.<br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=884dee5c0ed4731ee41779430c953841&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=884dee5c0ed4731ee41779430c953841&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.sitepoint.com/blogs/wp-content/uploads/2009/07/041-installing-php.jpg" alt="PHP5.3 on Windows" title="PHP5.3 on Windows" width="260" height="260" class="imgright" />With all the excitement surrounding <a href="http://www.sitepoint.com/blogs/2009/07/01/firefox-35-out-now/">Firefox 3.5</a>, it may have escaped your attention that PHP 5.3 has also been released. This is the most significant update since version 5.0 and several features that were originally scheduled for version 6.0 have been slipped into the build. You&#8217;ll be hearing about these on SitePoint soon but, first, we need to install PHP 5.3 on our Windows development systems.</p>
<h2>A Word of Warning&#8230;</h2>
<p>PHP 5.3 is new and you may experience some problems (<a href="#phperror">see below</a>). If you already have a stable PHP environment, ensure you keep a backup of your current php folder and settings.</p>
<h2>Web Server Installation</h2>
<div id="adz" class="vertical"></div><p>If you have not installed Apache, refer to <a href="http://www.sitepoint.com/blogs/2009/04/07/how-to-install-apache-on-windows/"><strong>How to Install Apache Web Server on Windows</strong>.</a> Optionally, you can also install the MySQL database &#8212; refer to <a href="http://www.sitepoint.com/blogs/2009/03/24/how-to-install-mysql/"><strong>How to Install MySQL</strong></a>.</p>
<h2>PHP 5.3 Installation</h2>
<p>Windows PHP distributions are now available from a dedicated microsite at <a href="http://windows.php.net/download/">http://windows.php.net/download/</a>. The choice is a little bewildering and the instructions are vague but, since we will be installing PHP as an Apache 2.2 module using the <a href="http://www.apachelounge.com/">Apache Lounge build</a>, so you should download the <strong>VC9 x86 Thread Safe</strong> Zip file.</p>
<p><em>General note:</em><br />
<em>Download the <strong>VC6</strong> builds if you are using the standard <a href="http://www.apache.org/">Apache.org</a> web server. The <strong>VC9</strong> builds should be used for the <a href="http://www.apachelounge.com/">Apache Lounge</a> binaries or IIS.</em></p>
<p><em>A <strong>Thread Safe</strong> version should be used if you install PHP as an Apache module. The <strong>Non Thread Safe</strong> version should be used if you install PHP as a CGI binary.</em></p>
<p>You can now follow the instructions at <a href="http://www.sitepoint.com/blogs/2009/04/13/how-to-install-php-on-windows/"><strong>How to Install PHP on Windows</strong></a> from step 2 onwards. Note that, in step 3, <em>php.ini-recommended</em> is now named <em>php.ini-development</em>; you still need make a copy and rename it <strong>php.ini</strong>.</p>
<p>If you have previously defined a working PHP configuration file, a tool such as <a href="http://winmerge.org/">WinMerge</a> can help you compare and copy existing values to your new php.ini file.</p>
<h2 id="phperror">PHP 5.3 Error Reporting</h2>
<p>The first time you load your previously bug-free PHP application, you are likely to be confronted by reams of long-winded error messages. Don&#8217;t panic! PHP 5.3 introduces a number of new error directives which are enabled by default in the development php.ini configuration:</p>
<ul>
<li>E_STRICT suggests improvements that ensure best interoperability and forward compatibility of your code, and</li>
<li>E_DEPRECATED warns you about code that will not work in future versions of PHP.</li>
</ul>
<p>Very useful. In my experience, the majority of messages report that a time zone has not been defined and assigning a return values using new by reference is deprecated.</p>
<p>Unfortunately, applications can display so many minor errors they become impossible to use. You can disable the new error directives by setting the error_reporting value to &quot;E_ALL &#038; ~E_DEPRECATED&quot; on line 514 of php.ini and restarting Apache. </p>
<p>However, several web applications, including WordPress, define their own error_reporting settings and may continue to show messages. It will take a developers some time to upgrade their applications to full PHP 5.3 compatibility.</p>
<p>Have you installed PHP 5.3 successfully? Have you experienced any major issues or problems with existing code?</p>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=136&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=884dee5c0ed4731ee41779430c953841&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=884dee5c0ed4731ee41779430c953841&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2009/07/07/install-php53-windows/feed/</wfw:commentRss>
			<slash:comments>16</slash:comments>
		</item>
		<item>
			<title>Ready for PHP &amp; MySQL Week at SitePoint?</title>
			<link>http://www.pheedcontent.com/click.phdo?i=b48cdc312193194cf77b65da31406cff</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2009/07/03/ready-for-php-mysql-week-at-sitepoint/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2009/07/03/ready-for-php-mysql-week-at-sitepoint/#comments</comments>
			<pubDate>Fri, 03 Jul 2009 04:35:02 +0000</pubDate>
			<dc:creator>Andrew Tetlaw</dc:creator>
			<category><![CDATA[PHP]]></category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=11891</guid>
			<description><![CDATA[To celebrate the release of the new edition of well-loved SitePoint book "Build Your Own Database Driven Web Site Using PHP &#038; MySQL, 4th Edition" — by Kevin Yank, we're publishing a chapter a day next week, beginning Tuesday the 7th of July. Check out what you'll be reading.<br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=b48cdc312193194cf77b65da31406cff&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=b48cdc312193194cf77b65da31406cff&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.sitepoint.com/books/phpmysql4/"><img class="imgright alignright" title="Build Your Own Database Driven Web Site Using PHP &amp; MySQL, 4th Edition" src="http://www.sitepoint.com/images/books/phpmysql4/blocks.jpg" alt="" /></a>To celebrate the release of the new edition of well-loved SitePoint book <em><a href="http://www.sitepoint.com/books/phpmysql4/">Build Your Own Database Driven Web Site Using PHP &amp; MySQL, 4th Edition</a></em> — by Kevin Yank, we&#8217;re publishing a chapter a day next week, beginning Tuesday the 7th of July.</p>
<p>Today, we&#8217;ve published the beginning of the series: <a href="http://www.sitepoint.com/article/php-mysql-tutorial/">the Introduction</a> and directly from the book, <a href="http://www.sitepoint.com/article/php-amp-mysql-1-installation/">Chapter 1: Installation</a>. Over the weekend you can read chapter 1 and make sure your newly installed software is working properly, in readiness for next week&#8217;s chapters. At the end of the week you&#8217;ll have <strong>4 complete chapters from the book and one bonus article</strong>, comprising another book excerpt.</p>
<p>Here&#8217;s what you&#8217;ll be receiving:</p>
<p><strong><a href="http://www.sitepoint.com/article/getting-started-mysql/">Chapter 2: Introducing MySQL</a></strong></p>
<div id="adz" class="vertical"></div><p>An introduction to databases in general, and the MySQL relational database management system in particular. If you&#8217;ve never worked with a relational database before, you&#8217;ll find this enlightening, whetting your appetite for what’s to come! In the process, you’ll build up a simple database to be used in later chapters.</p>
<p><strong><a href="http://www.sitepoint.com/article/mysql-3-getting-started-php/">Chapter 3: Introducing PHP</a></strong></p>
<p>Here’s where the fun really starts. In this chapter, Kevin will introduce you to the PHP scripting language, which you can use to build dynamic web pages that present up-to-the-moment information to your visitors.</p>
<p><strong><a href="http://www.sitepoint.com/article/publishing-mysql-data-web/">Chapter 4: Publishing MySQL Data on the Web</a></strong></p>
<p>In this chapter you’ll bring together PHP and MySQL to create some of your first database driven web pages. You’ll explore the basic techniques of using PHP to retrieve information from a database and display it on the Web in real time. Kevin will also show you how to use PHP to create web-based forms for adding new entries to, and modifying existing information in, a MySQL database on the fly.</p>
<p><strong><a href="http://www.sitepoint.com/article/handle-file-uploads-php/">Bonus: How to Handle File Uploads with PHP</a></strong><br />
In this bonus excerpt from Chapter 12, you&#8217;ll learn how to accept file uploads from your web site visitors securely and store them.</p>
<p><em><a href="http://www.sitepoint.com/books/phpmysql4/">Build Your Own Database Driven Web Site Using PHP &amp; MySQL, 4th Edition</a></em> is one of the most popular PHP books for beginners, and SitePoint&#8217;s first ever book. This shiny new 4th edition has been completely updated using only best-practice PHP. It&#8217;s essential reading for all budding PHP &amp; MySQL developers. For more information, see the <a href="http://www.sitepoint.com/books/phpmysql4/">book page</a>.</p>
<p>If you prefer to read the Adobe Acrobat PDF version of these first four chapters, you can <a href="http://www.sitepoint.com/books/phpmysql4/samplechapters.php">download the first four chapters FREE</a>.</p>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=136&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=b48cdc312193194cf77b65da31406cff&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=b48cdc312193194cf77b65da31406cff&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2009/07/03/ready-for-php-mysql-week-at-sitepoint/feed/</wfw:commentRss>
			<slash:comments>14</slash:comments>
		</item>
		<item>
			<title>Learn PHP The Easy Way</title>
			<link>http://www.pheedcontent.com/click.phdo?i=e98e6c21c68477f2dede7a401df9dda1</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2009/07/02/learn-php-the-easy-way/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2009/07/02/learn-php-the-easy-way/#comments</comments>
			<pubDate>Thu, 02 Jul 2009 10:48:44 +0000</pubDate>
			<dc:creator>ShayneTilley</dc:creator>
			<category><![CDATA[PHP]]></category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=11823</guid>
			<description><![CDATA[With over 40,000 copies sold of the three previous editions, I’m very pleased to announce the latest title hot off the SitePoint production line, <em>Build Your Own Database Driven Web Site Using PHP &#038; MySQL, 4th Edition</em>, by Kevin Yank.<br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=e98e6c21c68477f2dede7a401df9dda1&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=e98e6c21c68477f2dede7a401df9dda1&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><img class="imgright" src="http://www.sitepoint.com/images/books/phpmysql4/cover.jpg" alt="PHPMYSQL4 Cover" /><br />
With over 40,000 copies sold of the three previous editions, I’m very pleased to announce the latest title hot off the SitePoint production line:</p>
<p><strong><a href="http://www.sitepoint.com/books/phpmysql4/">Build Your Own Database Driven Web Site Using PHP &#038; MySQL (4th Edition)</a></strong> &#8212; by Kevin Yank</p>
<p>One of the most popular PHP books for beginners, this shiny new 4th edition has been completely updated using only best-practice PHP.  </p>
<p>In this book you’ll learn how to:</p>
<div id="adz" class="vertical"></div><ul>
<li>install PHP 5 and MySQL 5 on Windows, Linux, or Mac OS X</li>
<li>gain a thorough understanding of PHP syntax</li>
<li>master database design principles and SQL</li>
<li>build a working content management system</li>
<li>add, edit, and delete web content without using HTML</li>
<li>create an ecommerce shopping cart</li>
<li>utilize sessions and cookies to track site visitors</li>
<li>craft SEO-friendly and memorable URLs</li>
</ul>
<p>And a whole lot more &#8230; </p>
<p>At around 480 pages, this book is an easy read. As you move through the book you’ll quickly notice that it’s written in a clear tutorial format that’s easy to understand, and illustrated with plenty of screenshots and diagrams, providing quick visual cues. If you hate wading through dry, academic-style “how to” texts, this book will be a breath of fresh air to you.</p>
<p>If you’ve never built a database driven web site and you’re looking to go beyond the limitations of a static site, this book will start you off in no time. </p>
<p>And if you have built database driven web sites before, the extensive PHP/MySQL reference guides included will ensure this book remains an extremely handy desk reference.</p>
<p>Check out the <a href="http://www.sitepoint.com/books/phpmysql4/samplechapters.php">free sample chapters</a> or <a href="https://sitepoint.com/bookstore/go/157">order a copy today</a>.</p>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=136&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=e98e6c21c68477f2dede7a401df9dda1&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=e98e6c21c68477f2dede7a401df9dda1&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2009/07/02/learn-php-the-easy-way/feed/</wfw:commentRss>
			<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
<!-- Dynamic Page Served (once) in 1.522 seconds -->
<!-- Cached page served by WP-Cache -->