<?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"
	>

<channel>
	<title>SitePoint Blogs &#187; PHP</title>
	<atom:link href="http://www.sitepoint.com/blogs/category/tech/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sitepoint.com/blogs</link>
	<description></description>
	<pubDate>Fri, 10 Oct 2008 21:09:15 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
	<language>en</language>
			<item>
		<title>DOM vs. Template</title>
		<link>http://www.sitepoint.com/blogs/2008/09/25/dom-vs-template/</link>
		<comments>http://www.sitepoint.com/blogs/2008/09/25/dom-vs-template/#comments</comments>
		<pubDate>Wed, 24 Sep 2008 22:04:07 +0000</pubDate>
		<dc:creator>Troels Knak-Nielsen</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=3025</guid>
		<description><![CDATA[Fredrik Holmström recently posted a small template engine, based on DOM-manipulation. While there are certainly a lot of template engines around, I find this approach interesting. The concept is simple enough; The template is parsed into an object model (DOM), and then values can be assigned to these through PHP code. The main difference to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://loveandtheft.org/2008/09/17/pquery-messing-with-the-dom-from-php/">Fredrik Holmström recently posted a small template engine</a>, based on DOM-manipulation. While there are certainly a lot of template engines around, I find this approach interesting. The concept is simple enough; The template is parsed into an object model (<a href="http://en.wikipedia.org/wiki/Document_Object_Model">DOM</a>), and then values can be assigned to these through PHP code. The main difference to traditional template engines (Such as <a href="http://www.smarty.net/">Smarty</a>), is that the template it self doesn&#8217;t have any imperatives within. In fact, the template doesn&#8217;t even have to be written to the template engine, to be used - Any markup can be used as a source.</p>
<p>Since the template can&#8217;t contain any view-logic, it ends up in a separate place (In PHP code). This makes the separation between presentation and logic airtight, which was the main idea of template engines in the first place. Another benefit is that since there is no string-level manipulation, it is virtually impossible to inadvertently get injections-type security breaches.</p>
<p>The template may be unaware of the view-logic, but the opposite can&#8217;t be said. To bind values to the template, the view-logic needs to be aware of the internal structure of the template. This means that if the template changes, so must the view-logic. To decouple this dependency, we need some kind of abstraction.</p>
<p>Luckily it just so happens that there is a very convenient mechanism for that; Element <em>id</em>&#8217;s can be used to address central nodes in the markup. They do however have the rather annoying limitation (For this use), <a href="http://www.w3.org/TR/REC-html40/struct/global.html#adef-id">that they must be globally unique to the document</a>. A better candidate then, is to use classes (The HTML attribute - I&#8217;m not talking of PHP classes) to address elements.</p>
<div id="adz" class="horizontal"></div><p>The really nice thing about using classes is that it&#8217;s very unobtrusive to the markup. One will have to add classes, but since they would have to go on central elements in the markup, they would be prime candidates for reusing as fix points for CSS rules and for Javascript code. Instead of being superfluous markers in the HTML code, they actively help to write better markup.</p>
<p>That sounds good in theory, so to see how it holds out in reality, I mocked together a small prototype. Even with a very limited API, it has a remarkably good expressiveness:</p>
<h4>Simple variable binding</h4>
<pre><code>
$t = new Domling('&lt;p class="hello"&gt;&lt;/p&gt;');
$t-&gt;capture('hello')-&gt;bind("Hello World");
echo $t-&gt;render();
</code></pre>
<pre><code>
&lt;p class="hello"&gt;Hello World&lt;/p&gt;
</code></pre>
<h4>Switching a block out</h4>
<pre><code>
$t = new Domling('&lt;p&gt;Lorem Ipsum&lt;/p&gt;&lt;p class="message"&gt;Hidden message&lt;/p&gt;');
$t-&gt;capture('message');
echo $t-&gt;render();
</code></pre>
<pre><code>
&lt;p&gt;Lorem Ipsum&lt;/p&gt;
</code></pre>
<h4>Putting it back in</h4>
<pre><code>
$t = new Domling('&lt;p&gt;Lorem Ipsum&lt;/p&gt;&lt;p class="message"&gt;Hidden message&lt;/p&gt;');
$block = $t-&gt;capture('message');
$block-&gt;bind();
echo $t-&gt;render();
</code></pre>
<pre><code>
&lt;p&gt;Lorem Ipsum&lt;/p&gt;
&lt;p class="message"&gt;Hidden message&lt;/p&gt;
</code></pre>
<h4>And looping over a block</h4>
<pre><code>
$t = new Domling('&lt;ul class="links"&gt;&lt;li class="link"&gt;&lt;a class="anchor" href="#"&gt;title&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;');
$links = array(
  'Sitepoint' =&gt; 'http://www.sitepoint.com',
  'Example' =&gt; 'http://www.example.org?foo=bar&amp;ding=dong');
foreach ($links as $title =&gt; $link) {
  $t-&gt;sequence('link', 'links')-&gt;bind(array('anchor:href' =&gt; $link, 'anchor' =&gt; $title));
}
echo $t-&gt;render();
</code></pre>
<pre><code>
&lt;ul class="links"&gt;
&lt;li class="link"&gt;&lt;a class="anchor" href="http://www.sitepoint.com"&gt;Sitepoint&lt;/a&gt;&lt;/li&gt;
&lt;li class="link"&gt;&lt;a class="anchor" href="http://www.example.org?foo=bar&amp;amp;ding=dong"&gt;Example&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</code></pre>
<p>If you&#8217;re curious, you can get the full source code for the above examples from here: <a href="http://php.pastebin.com/f76ba8d70">http://php.pastebin.com/f76ba8d70</a></p>
<p>But please mind that this is just a proof-of-concept; There are probably a few quirks that should be ironed out before this could be used in production.</p>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=137&amp;did=adz&amp;adtype=horizontal" type="text/javascript"></script>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2008/09/25/dom-vs-template/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Character Encoding: Issues with Cultural Integration</title>
		<link>http://www.sitepoint.com/blogs/2008/09/10/issues-with-cultural-integration/</link>
		<comments>http://www.sitepoint.com/blogs/2008/09/10/issues-with-cultural-integration/#comments</comments>
		<pubDate>Tue, 09 Sep 2008 23:23:34 +0000</pubDate>
		<dc:creator>Troels Knak-Nielsen</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=2969</guid>
		<description><![CDATA[Ever been stuck trying to shoehorn UTF-8 encoded strings into a Latin1 character set? Troels encountered this problem recently, and this is how he tackled it.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve run into a classic problem with charsets, in an application I&#8217;m currently working on. As is the standard for PHP, all strings are treated as <a href="http://en.wikipedia.org/wiki/ISO_8859-1">latin1</a>, but we now need to allow a wider range of charsets in a few places.</p>
<p>The gold standard solution is to convert everything to utf-8. Since utf-8 covers the entire <a href="http://www.unicode.org/standard/WhatIsUnicode.html">unicode</a> range, it is capable of representing any character that latin1 can. Unfortunately, that&#8217;s a lot easier to do from the outset, than with a big, running application. And even then, there may be third party code and extensions, which assume latin1. I&#8217;d much rather continue with latin1 being the default, and only jump through hoops at the few places where I actually need full utf-8 capacity.</p>
<p>So after some thinking, another solution dawned on me. To be fair, <em>hack</em> is probably more descriptive than <em>solution</em>, but nonetheless. The idea goes as follows:</p>
<ul>
<li>Use latin1, but serve pages in utf-8, encoding it at output.</li>
<li>Embed utf-8 strings within latin1, and somehow don&#8217;t encode it (But still encode everything else).</li>
</ul>
<div id="adz" class="horizontal"></div><p>Simple, eh?</p>
<h2>Latin1 on the inside, utf-8 on the outside.</h2>
<p>When rendering HTML pages, it is trivial to capture the output with an <a href="http://www.php.net/outcontrol">output buffer</a> and pipe it through <a href="http://www.php.net/manual/en/function.utf8-encode.php">utf8_encode</a>. The page is thus served in utf-8, even though everything internally is latin1. Not much gain in that, since it still restricts us to use the range of characters covered by latin1.<br />
We are actually already doing this, simply to reduce the number of problems for external services communicating with our system. In particular, <a href="http://en.wikipedia.org/wiki/XMLHttpRequest">XmlHttpRequest</a> defaults to utf-8, regardless of the page&#8217;s encoding.</p>
<p>In essence, the following snippet exemplifies:</p>
<pre><code>
// declare that the output will be in utf-8
header("Content-Type: text/html; charset=utf-8");
// open an output buffer, capturing all output
ob_start('output_handler');
// when the script ends, the buffer is piped through this functions, encoding it from latin1 to utf-8
function output_handler($buffer) {
  return utf8_encode($buffer);
}
</code></pre>
<h2>Embed utf-8 within latin1.</h2>
<p>This is the tricky part. Instead of simply piping the entire buffer through utf8_encode, the string can be parsed so anything between a set of special tags (Eg. <code>[[charset:utf8]] ... [[/charset:utf8]]</code>) is left as-is, while the rest is assumed to be latin1 and encoded with utf8_encode as before. This ensures full backwards compatibility, while allowing real utf-8.</p>
<p>Let&#8217;s modify our output-handler from before:</p>
<pre><code>
header("Content-Type: text/html; charset=utf-8");
ob_start('output_handler');
function output_handler($buffer) {
  return preg_replace_callback(
    '~\[\[charset:utf8\]\](.*?)\[\[/charset:utf8\]\]~',
    'utf8_decode_first',
    utf8_encode($buffer));
}
function utf8_decode_first($match) {
  return utf8_decode($match[1]);
}
</code></pre>
<p>And that&#8217;s it. We can now embed full utf-8 strings within our otherwise latin1-encoded application, by wrapping it with <code>[[chaset:utf8]]</code>. To make things a bit more readable, I added a helper function:</p>
<pre><code>
function utf8($utf8_encoded_byte_stream) {
  return '[[charset:utf8]]' . $utf8_encoded_byte_stream . '[[/charset:utf8]]';
}
</code></pre>
<p>And we can now construct a string as simple as:</p>
<pre><code>
echo utf8("blÃ¥bÃ¦r") . "grød";
</code></pre>
<p>To produce the output: <strong>blåbærgrød</strong></p>
<p>
<b>note:</b> As pointed out by <a href="#comment-793844">Kore</a>, it would be a problem if the delimiter itself (Eg. <code>[[charset:utf8]]</code>) is part of the data. To remedy this, it would be safer to use a more unique delimiter. You could simply replace <code>charset:utf8</code> with something that is unlikely to ever happen. It&#8217;s still not completely bulletproof, but it&#8217;s good enough for most practical uses.
</p>
<h2>Handling input.</h2>
<p>You may or may not know this, but when submitting a form, browsers send back data in the same encoding as the page was served. Since our application is predominantly latin1, we need user-input to be latin1, to keep <abbr title="Backwards compatibility">BC</abbr>. So all input must be decoded from utf-8 to latin1. This is simple enough; We just have to pipe all user-input ($_GET, $_POST etc.) through <a href="http://www.php.net/manual/en/function.utf8-decode.php">utf8_decode</a>. Since we already run with the latin1-on-the-inside-utf-8-on-the-outside scheme, this was already in place in our case.</p>
<p>This does however give a problem when the user needs to submit utf-8, as our users would need when replying to mails. So in these places, we would have to explicitly access the &#8220;raw&#8221; string, through an alternate mechanism. In our case, we needed to modify our http-request wrapper, but since this is extending the API, there is no BC problems.</p>
<p>With the advent of PHP6, perhaps such hacks won&#8217;t be necessary in the future, but for now this gives a working, unobtrusive solution.</p>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=137&amp;did=adz&amp;adtype=horizontal" type="text/javascript"></script>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2008/09/10/issues-with-cultural-integration/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Rasmus Lerdorf: PHP Frameworks? Think Again.</title>
		<link>http://www.sitepoint.com/blogs/2008/08/29/rasmus-lerdorf-php-frameworks-think-again/</link>
		<comments>http://www.sitepoint.com/blogs/2008/08/29/rasmus-lerdorf-php-frameworks-think-again/#comments</comments>
		<pubDate>Thu, 28 Aug 2008 16:10:56 +0000</pubDate>
		<dc:creator>david.seth.p</dc:creator>
		
		<category><![CDATA[PHP]]></category>
<category>drupal</category><category>drupalcon</category><category>php</category><category>rasmus lerdorf</category>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=2914</guid>
		<description><![CDATA[ This is the fist time I have heard Rasmus Lerdorf speak and it was entertaining to say the least. Refreshing would another way to describe it, I enjoy hearing real opinions and not holding back &#8212; Rasmus doesn&#8217;t hold back. 
Just a short background, Rasmus Lerdorf is the creator of PHP and still continues [...]]]></description>
			<content:encoded><![CDATA[<p><img style="0px 0px 0px 10px" src="http://szeged2008.drupalcon.org/files/Rasmus.jpg" align="right" /> This is the fist time I have heard <a href="http://en.wikipedia.org/wiki/Rasmus_Lerdorf">Rasmus Lerdorf</a> speak and it was entertaining to say the least. Refreshing would another way to describe it, I enjoy hearing real opinions and not holding back &#8212; Rasmus doesn&#8217;t hold back. </p>
<p>Just a short background, Rasmus Lerdorf is the creator of PHP and still continues as a core developer to the PHP project. </p>
<p><strong>PHP frameworks</strong></p>
<p>In his address he choose to highlight PHP frameworks (Drupal was not spared) and how poor they are at performance. Not only are they slow, but their &quot;jack-of-all-trades&quot; attitude leads developers down the wrong path by not using what is best for the job. He continues on by stating that PHP developers really need to think about performance for not only scalability reasons but for green reasons. If programs were more efficient it would cut the number of data centres and would reduce energy needs as a result. In our newly emerging age of energy awareness this does become an important aspect and I am glad that he is raising awareness.</p>
<div id="adz" class="vertical"></div><p>Back to frameworks, he started by discussing a database heavy Twitter mashup that he created. This does a lot of database calls and a lot of behind the scenes work. By hand-tuning it he was able to get on the order of 280 req/sec. By comparison and simple HTML page with nothing but &quot;Hello World&quot; served by Apache is just over 600 req/sec. Okay, stage is set (by the way, this was tested on his local machine).</p>
<p><strong>Hello World</strong></p>
<p>How do PHP frameworks score on the &quot;Hello World&quot; test? No database calls, just the framework being used in its native tongue to output Hello World. The results were not too good, one of the fastest got just over 120 req/sec, the slowest was 8 req/sec. This is a dramatic difference and of course highlights his argument for performance. Where did Drupal score? Right above 50 req/sec. So not the greatest, but he did make the point that Drupal is not really a framework in the traditional sense. It is a web content management system that can be quickly extended.</p>
<p>So, are there any frameworks that don&#8217;t suck? Rasmus did mention that he liked <a href="http://codeigniter.com/">CodeIgniter</a> because it is faster, lighter and the least like a framework.</p>
<p><strong>How to make PHP fast</strong></p>
<p>&quot;Well, you can&#8217;t&quot; was his quick answer. PHP is simply not fast enough to scale to Yahoo levels. PHP was never meant for those sorts of tasks. &quot;Any script based language is simply not fast enough&quot;. To get the speed that is necessary for truly massive web systems you have to use compiled C++ extensions to get true, scaleable architecture. That is what Yahoo does and so do many other PHP heavyweights.</p>
<p><strong>RDF, Semantic Web and the Monkey</strong></p>
<p>RDF in Drupal. Rasmus made a special point of highlighting the importance of embedding structured    metadata into the page. RDFa allows you to embed data into your web pages and also lets you create custom vocabularies, or even better, reuse existing vocabularies. Why would you want to do this? Searchmonkey will go out and index this content and open up a rich search API to allow you to do intelligent queries. Well beyond what is possible with traditional search. </p>
<p>Along with rich search you also get enhanced search results. I have blogged about this previously so take a <a href="http://www.sitepoint.com/blogs/2008/03/16/why-rdfa-is-the-only-web-scaleable-metadata-format-for-next-generation-search-engines/">look</a>. It is really cool stuff and I will be discussing it in much more detail over the course of the conference.</p>
<p><strong>Pitching the Semantic Web</strong></p>
<p>What if all Drupal sites had embedded RDFa tags? Well, for one, Yahoo would be very happy. It would play directly into the strengths of Yahoo&#8217;s new Semantic Web strategy. They are trying to do interesting things with semantic data but of course they need data &#8212; the classic chicken and egg thing.</p>
<p>Rasmus mentioned that Yahoo&#8217;s semantic data store can scale to the size of the web so the invitation is open. </p>
<p><strong>The future of Drupal</strong></p>
<p>This is where my focus at Drupalcon is, driving the adoption of semantic technologies within Drupal &#8212; I feel that the momentum here will make that a reality. There is a lot of interest, a Semantic Web BoF session was stacked with people with some cool ideas&#8230;</p>
<p>More to come.</p>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=136&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2008/08/29/rasmus-lerdorf-php-frameworks-think-again/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Mangling XML as Text with PHP DOM</title>
		<link>http://www.sitepoint.com/blogs/2008/07/24/mangling-xml-as-text-with-php-dom/</link>
		<comments>http://www.sitepoint.com/blogs/2008/07/24/mangling-xml-as-text-with-php-dom/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 06:46:09 +0000</pubDate>
		<dc:creator>brothercake</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=2703</guid>
		<description><![CDATA[
Recently I had to do some mass-conversion of HTML files to DITA XML &#8212; material I&#8217;d written for the upcoming JavaScript Ultimate Reference (the third, and arguably most complicated, part of the SitePoint Reference).


But a problem I came across several times was the sheer complexity of recursive element conversion &#8212; &#60;code&#62; becomes &#60;jsvalue&#62; (or one [...]]]></description>
			<content:encoded><![CDATA[<p>
Recently I had to do some mass-conversion of <abbr title="HyperText Markup Language">HTML</abbr> files to <a href="http://dita.xml.org/">DITA XML</a> &#8212; material I&#8217;d written for the upcoming JavaScript Ultimate Reference (the third, and arguably most complicated, part of the <a href="http://reference.sitepoint.com/">SitePoint Reference</a>).
</p>
<p>
But a problem I came across several times was the sheer complexity of recursive element conversion &#8212; <code>&lt;code&gt;</code> becomes <code>&lt;jsvalue&gt;</code> (or one of a dozen similar elements), <code>&lt;a&gt;</code> becomes <code>&lt;xref&gt;</code> &#8230; and that&#8217;s all simple enough; but each of these elements might contain the other, or further child elements like <code>&lt;em&gt;</code>, and as we walk through the <abbr title="Document Object Model">DOM</abbr> so the incidence of potential recursion increases, until it gets to the point where my brain explodes.
</p>
<p>
There&#8217;s a limit to how much recursion I can get my head around &#8212; or rather &#8212; a limit to how much I&#8217;m <em>prepared</em> to get my head around before I just go <q>the heck with this, why can&#8217;t I mangle it as text with regular expressions!?</q>
</p>
<p>
Unfortunately there doesn&#8217;t seem to be a way with <abbr title="HyperText Pre-Processor">PHP</abbr> <abbr title="Document Object Model">DOM</abbr> to get the text equivalent of any arbitrary node, but we can do that at the <code>Document</code> or <code>DocumentFragment</code> level; so with a little toying-around I came up with a way to leverage that capability and make it work at the <code>Node</code> level.
</p>
<p>So for example, let&#8217;s begin with this <abbr title="eXtensible Markup Language">XML</abbr>:</p>
<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;root id="introduction"&gt;
	&lt;div class="section"&gt;
		The fundamental data type is &lt;code&gt;Node&lt;/code&gt;
	&lt;/div&gt;
&lt;/root&gt;</code></pre>
<p>We have a reference to its <abbr title="Document Object Model">DOM</abbr> saved to a <abbr title="HyperText Pre-Processor">PHP</abbr> variable called <code>$xmldom</code>. And we want to parse it so that the <code>&lt;code&gt;</code> element becomes a <code>&lt;jstype&gt;</code>, and the <code>&lt;div class="section"&gt;</code> becomes simply <code>&lt;section&gt;</code>, all without affecting the rest of the document.</p>
<p>Here&#8217;s the complete code to do it, which I&#8217;ll then talk through stage by stage:</p>
<pre><code>$node = $xmldom-&gt;documentElement-&gt;firstChild;

$doc = new DOMDocument();
$doc-&gt;loadXML('&lt;xmltext/&gt;');
$node = $doc-&gt;importNode($node, true);
$doc-&gt;documentElement-&gt;appendChild($node);
$xmltext = ereg_replace('^.*&lt;xmltext&gt;(.*)&lt;\/xmltext&gt;.*$', '\\1', $doc-&gt;saveXML());

$xmltext = ereg_replace('&lt;([\/]?)code&gt;', '&lt;\\1jstype&gt;', $xmltext);
$xmltext = ereg_replace('&lt;([\/]?)div[^&gt;]*&gt;', '&lt;\\1section&gt;', $xmltext);

$node = $xmldom-&gt;createDocumentFragment();
$node-&gt;appendXML($xmltext);

$xmldom-&gt;documentElement-&gt;replaceChild($node, $xmldom-&gt;documentElement-&gt;firstChild);</code></pre>
<p>
In the first step we get a reference to the element we want to work with, and save it to <code>$node</code>.
</p>
<div id="adz" class="vertical"></div><p>
In the second step we create a new document, and use <code>loadXML()</code> to create a placeholder root node (the <a href="http://au.php.net/manual/en/domdocument.loadxml.php"><code>loadXML</code>  method</a> converts text input to <abbr title="eXtensible Markup Language">XML</abbr>, and is one of the cornerstones of our process). Next we import the original node into that document, then use <code>saveXML()</code> to convert the whole document to text (the <a href="http://au.php.net/manual/en/domdocument.savexml.php"><code>saveXML</code> method</a> converts an XML document to text, and is as critical as <code>loadXML()</code> for what we&#8217;re doing here). The text output is parsed using <a href="http://au.php.net/manual/en/function.ereg-replace.php"><code>ereg_replace</code></a> to remove the outer contents of the document (its prolog and root node) so that we&#8217;re left with a text equivalent of the original input node.
</p>
<p>
In the third step we do whatever text-based mangling we need; in this case it&#8217;s simple element name conversions, but it could be anything.
</p>
<p>
In the fourth step we want to convert our parsed text back into <abbr title="eXtensible Markup Language">XML</abbr>, and we do this by creating a document fragment, then using <code>appendXML()</code> to load the text and have it converted to <abbr title="eXtensible Markup Language">XML</abbr> (the <a href="http://au.php.net/manual/en/domdocumentfragment.appendxml.php"><code>appendXML</code> method</a> does the same thing as <code>loadXML()</code>, but it doesn&#8217;t require an entire document to be created).
</p>
<p>
Finally, in the fifth step we merge the processed <abbr title="eXtensible Markup Language">XML</abbr> back into our original document. The document fragment has the original document as its owner, so we can simply use the <a href="http://au.php.net/manual/en/domnode.replacechild.php"><code>replaceChild</code></a> method to replace the original node and its children with the processed version. (Whenever a document fragment is added to a document, only its children are actually added, the document fragment itself is discarded; <code>DocumentFragment</code> is a virtual construct and never actually appears in a document.)
</p>
<p>
Both the first and the final step are arbitrary &#8212; we could work with an entire document, or just a single node, and edit our referencing and merging statements accordingly. Or we could build a method from the inner steps, which accepts <code>$node</code> as an argument (and maybe an array of replacement expressions), and returns the processed node at the end:
</p>
<pre><code>function mangleXML($node)
{
	...
	
	return $node;
}</code></pre>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=136&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2008/07/24/mangling-xml-as-text-with-php-dom/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Keeping Current With PHP</title>
		<link>http://www.sitepoint.com/blogs/2008/07/16/keeping-current-with-php/</link>
		<comments>http://www.sitepoint.com/blogs/2008/07/16/keeping-current-with-php/#comments</comments>
		<pubDate>Tue, 15 Jul 2008 23:09:08 +0000</pubDate>
		<dc:creator>Troels Knak-Nielsen</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=2653</guid>
		<description><![CDATA[Just a quick hint to people with an interest in the development of PHP, but no time for following php-internals. Since March, there has been a wiki at wiki.php.net. The most interesting section is probably wiki.php.net/rfc, which - as the name implies - contains RFC&#8217;s for improvements of the language. I&#8217;ve rambled on about closures [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick hint to people with an interest in the development of PHP, but no time for following <a href="http://news.php.net/php.internals">php-internals</a>. Since March, there has been a wiki at wiki.php.net. The most interesting section is probably <a href="http://wiki.php.net/rfc">wiki.php.net/rfc</a>, which - as the name implies - contains <a href="http://en.wikipedia.org/wiki/Request_for_Comments" title="Request for Comments">RFC</a>&#8217;s for improvements of the language. <a href="http://www.sitepoint.com/blogs/2007/12/23/lexical-scope-to-appear-in-php/">I&#8217;ve rambled on about closures and lambdas</a> before, but as you can see, there is now an accepted <a href="http://wiki.php.net/rfc#accepted">patch</a>. Whether it&#8217;ll make it into 5.3 is unlikely at this point, but it looks like it&#8217;ll at least be coming with 5.4 and/or 6.0.</p>
<p>While we&#8217;re at it, <a href="http://devzone.zend.com/member/83-steph-fox-staff">Steph Fox</a> have dutifully been writing up <a href="http://devzone.zend.com/tag/Weekly_Summaries">summaries of php-internals discussions</a> on a weekly basis since the beginning of time. If you just want to get the headlines, it&#8217;s an informative read.</p>
<p>Have a nice summer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2008/07/16/keeping-current-with-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Last we checked, PHP IS a framework.</title>
		<link>http://www.sitepoint.com/blogs/2008/06/08/last-we-checked-php-is-a-framework/</link>
		<comments>http://www.sitepoint.com/blogs/2008/06/08/last-we-checked-php-is-a-framework/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 04:18:58 +0000</pubDate>
		<dc:creator>Akash Mehta</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=2539</guid>
		<description><![CDATA[When it comes to web programming languages, PHP probably holds the record for copping criticism from the community at large. Comparisons with alternatives such as Ruby on Rails and Python/Django are common; defenders of PHP are quick to criticise the comparison of a language and a framework. But at the end of the day, developers [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.sitepoint.com/blogs/wp-content/uploads/2008/06/phpframework1.jpeg" alt="" width="130" height="70" class="imgright" />When it comes to web programming languages, PHP probably holds the record for copping criticism from the community at large. Comparisons with alternatives such as Ruby on Rails and Python/Django are common; defenders of PHP are quick to criticise the comparison of a language and a framework. But at the end of the day, developers work with Ruby on Rails, and with Python/Django, and with PHP. Just PHP. For most of the PHP applications out there, the language is just perfect, because PHP, to an extent, <em>is</em> the framework.</p>
<p>PHP is designed for the web. You could plug vanilla Ruby or Python into a web server and get up and running pretty quickly. But, at least at a basic level, you&#8217;d want a framework to deal with common issues of web development. In PHP, you just get started. PHP and Apache work out request data, output handling and more, right out of the box. (<a href="http://www.sitepoint.com/blogs/2008/01/13/what-php-deployment-gets-right-ian-bicking-nails-it-at-last/">PHP also masters deployment.</a>) David Heinemeier Hanson, the creator of the Ruby on Rails framework, <a href="http://www.loudthinking.com/posts/23-the-immediacy-of-php">calls this the immediacy of PHP</a>.</p>
<p>Now, consider the &#8220;average&#8221; PHP frameworks. They help you handle request data, manage your output, control app flow - essentially, extending PHP&#8217;s inbuilt functionality. They are, therefore, PHP frameworks on the PHP framework. PHP provides a vast array (pardon the pun) of functionality out of the box. But when you want to do things your way, it gets out of the way, and this is really important when building anything beyond a simple database frontend.</p>
<p>How many PHP applications in wide distribution are built on a third-party framework? MediaWiki? No framework here. Wordpress? Not here either. Drupal? You get the idea. Each of these applications have their own framework, inasfar that the developers built a structure for their code that suited what they were trying to create. These three, and the countless others, clearly did something right - MediaWiki even powers <a href="http://en.wikipedia.org/">one of the world&#8217;s top 10 websites</a>. And when building these &#8220;frameworks&#8221;, PHP helped them along the way.</p>
<div id="adz" class="vertical"></div><p>Sure, Cake, Symfony, CI - they all help you build PHP applications. But unlike a Ruby framework or a Python framework, coding is perfectly tolerable without them. Of course, most developers tend to create their own framework as they go along - I call this PHP&#8217;s <em>DIY framework</em> mentality, where you build the last level in your stack, and by extension you know exactly what&#8217;s going on under the hood.</p>
<p>This comes back to the oft-cited &#8220;best tool for the job&#8221; argument - but by being a &#8220;half framework&#8221; of sorts, PHP simply helps you within a web context. I&#8217;m sure Rails is great for countless applications; Ruby syntax helps a little too. But beyond the basics, the language becomes increasingly irrelevant, and a framework will force you to build your app according to design decisions made by the original developers. On the other hand, PHP allows you to quickly put together your application the way you want, helping you with the web-based aspect, and <a href="http://www.oreillynet.com/ruby/blog/2007/09/7_reasons_i_switched_back_to_p_1.html">without having to break the language/framework to achieve your goals</a>. Essentially, <em>PHP is the web layer without the framework baggage.</em></p>
<p>Twitter <a href="http://www.sitepoint.com/blogs/2008/06/06/did-rails-sink-twitter/">recently had some downtime that could be attributed to its use of Ruby on Rails</a>. Now, there&#8217;s nothing wrong with Ruby, or Rails. But Twitter is a messaging system, RoR is an application framework. Could Rails be worked around to suit Twitter? Maybe. Could Twitter have avoided these problems if they&#8217;d used PHP? Probably. Twitter&#8217;s developers have noted they weren&#8217;t quite expecting the site to be so succesful. If they had built it in PHP, they would probably have encountered other (similar) problems earlier on, rewritten the entire app while it was still feasible, and built a powerful system that could scale just fine (likely with the messaging platform in C). Using PHP could have helped, not as a language with syntax and libraries as such, but as a web development platform that gives developers the freedom to architect their systems as they want. (<a href="http://www.facebook.com/notes.php?id=9445547199">They could also have tried Erlang</a>.)</p>
<p>So the next time you&#8217;re on the lookout for a PHP framework to build your killer app on, consider using the best framework of them all: PHP. It will give you the freedom to achieve what you want, how you want, with results. A quick hack? Check the manual, it could be a one-liner. A massive distributed messaging platform? Full steam ahead, build it your way. For everything in between, there&#8217;s a multitude of options, but if you have the time, PHP is all the framework you need.</p>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=136&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2008/06/08/last-we-checked-php-is-a-framework/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHPBench.com: Live PHP benchmarks, demystifying &#8220;best practices&#8221;</title>
		<link>http://www.sitepoint.com/blogs/2008/06/02/phpbench-live-php-benchmarks-best-practices/</link>
		<comments>http://www.sitepoint.com/blogs/2008/06/02/phpbench-live-php-benchmarks-best-practices/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 11:49:39 +0000</pubDate>
		<dc:creator>Akash Mehta</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=2522</guid>
		<description><![CDATA[As a new contributor to the SitePoint blogs, I&#8217;ll be covering PHP web development, JavaScript and general web tech.
When it comes to optimizing PHP for performance, there&#8217;s no end of resources available, and no end of conflicting opinions either. Everyone seems to have their own approach to writing &#8220;fast&#8221; PHP code; using single quotes, avoiding [...]]]></description>
			<content:encoded><![CDATA[<p><em>As a new contributor to the SitePoint blogs, I&#8217;ll be covering PHP web development, JavaScript and general web tech.</em></p>
<p>When it comes to optimizing PHP for performance, there&#8217;s <a href="http://reinholdweber.com/?p=3">no</a> <a href="http://phplens.com/lens/php-book/optimizing-debugging-php.php">end</a> <a href="http://www.moskalyuk.com/blog/php-optimization-tips/1272">of</a> <a href="http://www.performancewiki.com/faster-php-code.html">resources</a> <a href="http://www.joomlaperformance.com/articles/performance/52_php_programming_tips_43_14.html">available</a>, and no end of conflicting opinions either. Everyone seems to have their own approach to writing &#8220;fast&#8221; PHP code; using single quotes, avoiding require_once() and using isset() before is_array() are some of the most common. But with reliable benchmarks thin on the ground, how do we know if any of these techniques - often touted as &#8220;performance best practices&#8221; - actually deliver benefits? Chris Vincent&#8217;s new PHP benchmark suite at <a href="http://www.phpbench.com/">PHPBench.com</a> aims to &#8220;set the record straight&#8221; on PHP performance techniques, with a simple, comprehensive view of how various approaches actually stack up.<br />
<span id="more-2522"></span><br />
The benchmark suite covers all the usual bases, taking a simple task &#8212; like iterating over an array &#8212; and speed testing almost every possible way to achieve it. Most importantly, however, Chris takes raw numbers out of the spotlight and instead focuses on how the options compare with each other. Each test takes the fastest technique as the base value for execution time; all the other options are measured as percentages in relation to this. For example, <code>foreach($aHash as $val)</code> has script execution time of 558% compared to <code>while(list($key) = each($aHash)) $tmp[] = $aHash[$key]</code> with 100%.</p>
<p>The test scores are also generated live; refreshing the page will produce a slightly different set of results. Chris uses the header of the relatively uncomplicated results page to recommend refreshing a few times, just to ensure consistency and to avoid a one-off impact on any particular test. Personally, I&#8217;d have preferred the tests be carried out in a controlled environment and regenerated every time a new test is added; anomalies don&#8217;t help anyone and live benchmarking offers little real benefit besides operational simplicity. The code for each test is supplied for transparency.</p>
<p>His current set of tests are very comprehensive; no less than 13 tests compared the performance of various <code>echo</code> and <code>print</code> calls, with enough variety to check for any usage scenario. He&#8217;s also accepting suggestions for new tests. Most importantly, however, many of his tests help identify the value of best practices. For example, PHP is often criticised for the vague results of <code>==</code> comparisons; the control structures tests show us that not only is <code>===</code> safer, it also takes about 2/3 as long to execute.</p>
<p>While most of the tests show trivial performance differences, there are some interesting results. For example, iterating over an array and modifying each element is 100 times as fast using <code>while(list($key) = each($aHash))</code> than a simple <code>foreach ($aHash as $key=&gt;$val)</code>, while <code>array_keys()</code> is about 20 times slower than <code>while(list($key,$val) = each($aHash));</code>.</p>
<div id="adz" class="vertical"></div><p>The next step, of course, would be to download the test suite to your local machine or server and run the tests yourself, to see how the factors of your production environment affect the results; Chris aims to make this possible in the near future. Many of the techniques listed are clearly going to be either memory-efficient or CPU cycle efficient, and the limits of your infrastructure will determine which way you want to go before scaling out. Nevertheless, managing bottlenecks in your code can really help you get the most out of your servers &#8212; after all, if you can achieve something 100 times as fast, why not? &#8212; and PHPBench.com could become the definitive reference on PHP performance.</p>
<p><strong>Update:</strong> Many commenters have noted issues with the usefulness and reliability of the tests. It&#8217;s important to consider Chris&#8217; intentions while creating the site, as he&#8217;s <a href="http://www.sitepoint.com/blogs/2008/06/02/phpbench-live-php-benchmarks-best-practices/#comment-738130">described in the comments</a>. With all the &#8220;use this syntax because it&#8217;s slightly faster&#8221; posts, PHPBench.com was built to see if there is any real material benefit to using a particular syntax; most of the tests show that there clearly isn&#8217;t. I&#8217;ve done a few tests of my own and seen similar results, but if something looks amiss, feel free to point it out to Chris. The site is not trying to identify immaterial performance benefits from e.g. single quotes vs. double quotes. Moving further, PHP Bench could be used to measure algorithm performance and other more intensive procedures; for now, the basic syntax tests simply reflect the potential of the system. Hopefully this clears things up.</p>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=136&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2008/06/02/phpbench-live-php-benchmarks-best-practices/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Debugging PHP</title>
		<link>http://www.sitepoint.com/blogs/2008/06/02/debugging-php/</link>
		<comments>http://www.sitepoint.com/blogs/2008/06/02/debugging-php/#comments</comments>
		<pubDate>Sun, 01 Jun 2008 23:55:54 +0000</pubDate>
		<dc:creator>Troels Knak-Nielsen</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=2523</guid>
		<description><![CDATA[I just checked in a little project, I&#8217;ve been working on for the last couple of weeks.
It really begun at the last Copenhagen php-meetup; Joakim Nygård and Jacob Oettinger made a presentation of their project, WebCacheGrind. I casually mentioned something about having played with the dbgp-protocol (The debugging part of Xdebug) and that it ought [...]]]></description>
			<content:encoded><![CDATA[<p>I just <a href="http://code.google.com/p/spectator/">checked in a little project</a>, I&#8217;ve been working on for the last couple of weeks.</p>
<p>It really begun at the last <a href="http://php.meetup.com/360/">Copenhagen php-meetup</a>; <a href="http://jokke.dk/">Joakim Nygård</a> and <a href="http://oettinger.dk/">Jacob Oettinger</a> made a presentation of their project, <a href="http://code.google.com/p/webgrind/">WebCacheGrind</a>. I casually mentioned something about having played with the dbgp-protocol (The debugging part of <a href="http://www.xdebug.org/">Xdebug</a>) and that it ought to be simple enough to write a fontend for it. That prompted some snickering from the back row. Apparently some guys had this rule at their workingplace, that whomever said that something <em>ought to be easy to do</em>, had to do so himself. Not an unreasonable rule, I suppose. Obviously, I couldn&#8217;t let that go unattended, so I gave it a shot.</p>
<p><a href='http://www.sitepoint.com/blogs/wp-content/uploads/2008/06/spectator-small1.png'><img src="http://www.sitepoint.com/blogs/wp-content/uploads/2008/06/spectator-small1.png" alt="Screenshot of Spectator" width="320" height="227" class="alignnone size-full wp-image-2525" /></a></p>
<p>Spectator is a XUL application, which should make it cross platform. I have tinkered a bit with XUL before, but not a full application. If you&#8217;re wondering what <a href="http://www.mozilla.org/projects/xul/">XUL</a> is, it&#8217;s the GUI toolkit, in which the frontends for Firefox and Thunderbird are written. It&#8217;s a markup language &#8212; much like HTML, which can be scripted with Javascript. This makes it very easy to work with. The only problem seems to be a rather lacking documentation, but a bit of detective work got me through that.</p>
<div id="adz" class="horizontal"></div><p>So what can spectator do? Mind that this is a first version and I really just meant it as a proof of concept. I think I got a bit further than that, but it probably still has a few bugs. Still, with the current version, you can step through a program, set breakpoints and inspect the stack. Really all you would expect from a debugger.</p>
<p>Of course, as some of you might point out, there are already other implementations available. Most notably <a href="http://activestate.com/Products/komodo_ide/index.mhtml?src=AScom&amp;type=bn&amp;X=HP&amp;campaign=KMD">Komodo</a>, which is also implemented with XUL. However, these implementations are proprietary, and are integrated with an IDE, that you may not want to use. Spectator is an open source alternative and is IDE/editor agnostic.</p>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=137&amp;did=adz&amp;adtype=horizontal" type="text/javascript"></script>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2008/06/02/debugging-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Useful in-browser development tools for PHP</title>
		<link>http://www.sitepoint.com/blogs/2008/05/13/useful-in-browser-development-tools-for-php/</link>
		<comments>http://www.sitepoint.com/blogs/2008/05/13/useful-in-browser-development-tools-for-php/#comments</comments>
		<pubDate>Mon, 12 May 2008 21:47:56 +0000</pubDate>
		<dc:creator>Troels Knak-Nielsen</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=2478</guid>
		<description><![CDATA[While debuggers exists, there isn&#8217;t much of a tradition for using them in PHP. People have largely come to rely on injecting debugging code directly into the program, for inspecting program scope. The infamous var_dump have served for this purpose and version 4.3.0 of PHP brought us another equally useful function &#8212; debug_backtrace.
Tracers and error [...]]]></description>
			<content:encoded><![CDATA[<p>While debuggers exists, there isn&#8217;t much of a tradition for using them in PHP. People have largely come to rely on injecting debugging code directly into the program, for inspecting program scope. The infamous <a href="http://docs.php.net/var_dump">var_dump</a> have served for this purpose and version 4.3.0 of PHP brought us another equally useful function &#8212; <a href="http://docs.php.net/debug_backtrace">debug_backtrace</a>.</p>
<h2>Tracers and error handlers</h2>
<p>Both of these functions produce a rather crude output though, so naturally people have written wrappers around them to remedy this. I think <a href="http://www.sitepoint.com/blogs/2006/04/04/pretty-blue-screen/">Harry&#8217;s pretty bluescreen</a> was one of the first dedicated libraries I&#8217;ve seen. <a href="http://xdebug.org/">Xdebug</a> spouts a similar output on error, although arguably not as pretty. Or blue.</p>
<p>What <em>bluescreen</em> is for <code>debug_backtrace</code>, <a href="http://krumo.sourceforge.net/" title="Version 2.0 of print_r(); and var_dump();">krumo</a> is for <code>var_dump</code>. Recently, <a href="http://www.firephp.org/" title="FirePHP - Firebug Extension for AJAX Development">FirePHP</a> &#8212; building on <a href="http://www.getfirebug.com/" title="Firebug - Web Development Evolved">Firebug</a> &#8212; does a similar thing. FirePHP uses HTTP-headers to send data from server to client, which turns out to be very handy when dealing with non-HTML output (Eg. Ajax stuff). Because it builds on Firebug, it only works on <a href="http://www.mozilla.com/firefox/" title="Firefox web browser">Firefox</a>, and in particular only on Firefox 2 (Another reason for <a href="http://www.ubuntu.com/" title="Ubuntu is a free, Debian derived Linux-based operating system">Ubuntu</a>-users to downgrade from Firefox 3).</p>
<div id="adz" class="vertical"></div><h2>Frameworks</h2>
<p>Apart for these general general tracing tools, a couple of frameworks have their own, more or less specific, tools. <a href="http://www.symfony-project.org/">Symfony&#8217;s</a> <a href="http://morethanseven.net/posts/nice-bits-of-symfony-web-debug-toolbar/">Debug Toolbar</a> is probably the most impressive (And in contrast, <a href="http://framework.zend.com/manual/en/zend.debug.html">Zend Framework the least</a>), even providing information about database-queries, which is very helpful for profiling.</p>
<p>Another framework-specific tool, which I&#8217;m quite impressed by, is the <a href="http://drupal.org/" title="drupal.org - Community plumbing<br />
&#8220;>Drupal</a> <a href="http://drupal.org/node/209561">Theme developer module</a>. It&#8217;s advertised as &#8220;Firebug for Drupal theming&#8221;, which is fairly descriptive. In case you haven&#8217;t used Drupal, it has a complex theming system for rendering pages and this tool makes it a lot easier to figure out which part is doing what. Even if you don&#8217;t intend on using Drupal, it&#8217;s fascinating to see how well polished it looks.</p>
<p>I think that most frameworks with an integrated presentation layer, could benefit hugely from something similar, although the specifics would obviously vary. One of my own projects, <a href="http://konstrukt.dk/">Konstrukt</a>, has a tool for displaying debug-information about which controllers renders in which order. This can be helpful, because a given page will often have several controllers working in concert to produce the final response.</p>
<p>Another take on this, is Cake PHP&#8217;s <a href="http://www.archive.org/details/UsingDebugMessages">debug messages</a>. This extension adds very specific and descriptive error messages in various places, which almost works out like a tutorial for new users; Seems like a good idea.</p>
<h2>Webgrind</h2>
<p>I already mentioned Xdebug, which is indispensable for profiling PHP applications. Last week, <a href="http://code.google.com/p/webgrind/">Webgrind</a> had its first released. It&#8217;s a HTML-based frontend for displaying profiling output, generated by Xdebug. There are already tools for the purpose; If you&#8217;re on Windows, <a href="http://sourceforge.net/projects/wincachegrind/">WinCacheGrind</a> and for Linux-based systems, <a href="http://kcachegrind.sourceforge.net/">kCacheGrind</a>. However, I can see a few good cases for Webgrind;</p>
<p>First of all, a zero-install client is a bit easier to reach out for. For Mac users, it&#8217;s also the only option. The featureset is quite limited, but this turns out to be somewhat a benefit. kCacheGrind can be quite intimidating and for the purpose of profiling, Webgrind&#8217;s features seems fairly adequate.<br />
The most important though, is that the logfile generated by Xdebug (Which is the data-format for the various grind&#8217;s) tend to get rather huge. Naturally, this makes it awkward to have server and client on separate machines &#8212; This is not a problem for Webgrind, since it runs at the server side.</p>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=136&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2008/05/13/useful-in-browser-development-tools-for-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A PHP Guy’s Look At Python</title>
		<link>http://www.sitepoint.com/blogs/2008/05/09/a-php-guy%e2%80%99s-look-at-python/</link>
		<comments>http://www.sitepoint.com/blogs/2008/05/09/a-php-guy%e2%80%99s-look-at-python/#comments</comments>
		<pubDate>Fri, 09 May 2008 08:24:57 +0000</pubDate>
		<dc:creator>Kevin Yank</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=2474</guid>
		<description><![CDATA[Against all odds, I found myself with a little spare time this week. Rather than do something sensible like clean the garage or get some exercise, I took the opportunity to learn a new programming language: Python.
Like may SitePoint readers, I cut my teeth on PHP. I’ve become very comfortable with it over the years, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Against all odds, I found myself with a little spare time this week. Rather than do something sensible like clean the garage or get some exercise, I took the opportunity to learn a new programming language: Python.</strong></p>
<p>Like may SitePoint readers, I cut my teeth on PHP. I’ve become very comfortable with it over the years, warts and all. PHP continues to be a dependable choice, but PHP hasn’t changed a whole lot lately. Meanwhile, the kinds of applications I’ve been working on have been growing dramatically in both size and complexity.</p>
<p>Python has a lot in common with PHP: it’s a dynamically typed, open source scripting language with excellent documentation and a thriving community around it. Both languages are also a little quirky when it comes to their handling of Unicode text. </p>
<p>Unlike PHP, Python wasn’t originally designed as a language for Web development—it’s a general programming language that just happens to have some excellent libraries and frameworks for building web sites, like <a href="http://www.djangoproject.com/">Django</a>. This may sound like an argument <em>against</em> Python, but it turns out that when you start writing bigger web applications, most of your code has nothing to do with HTML, and PHP’s HTML-friendly features just seem to get in the way.</p>
<h2>Enough Hand-waving! What’s The Code Like?</h2>
<p>Let’s look at some of the neat features of Python code.</p>
<p>The most distinguishing feature of Python code is the lack of braces or other delimiters around code blocks. Instead, Python uses the indenting of your code to indicate blocks:</p>
<pre><code class="python">print "Let's count to ten!"
for i in range(10):
  number = i + 1
  print number
print "All done!"</code></pre>
<p>The above code includes a <code>for</code> loop that will run ten times. The two lines following the <code>for</code> statement are indented to indicate they’re within the <code>for</code> loop. The last line isn’t indented, so Python knows the <code>for</code> loop is finished.</p>
<p>At first it feels frighteningly fragile to trust indentation to describe the structure of your code; but once you get used to it, you’ll notice that your code looks less cluttered. Even better, you’ll find it easier to read Python code written by others, because developers are <em>forced</em> to indent their code neatly and consistently.</p>
<p>Python has a number of convenient, little features that make common tasks less cumbersome than in other languages. Take multiple assignment, for example, which lets you avoid creating temporary variables:</p>
<pre><code class="python">a = 1
b = 2
print a, b     # prints '1 2'
a, b = b, a+b
print a, b     # prints '2 3'</code></pre>
<p>Python also has a number of slick features for dealing with what it calls sequences. These let you split and combine lists (the equivalent of PHP arrays) and text strings using simple, consistent syntax, instead of having to remember obscure function names for each.</p>
<p>Python’s list comprehensions let you quickly build complex lists out of simple lists with a minimum of code. They’re a little hard to understand at first glance, but they quickly become indispensable:</p>
<pre><code class="python">me = 'Kevin Yank'
range(5)                                 # [0, 1, 2, 3, 4]
[me[x] for x in range(5)]                # ['K', 'e', 'v', 'i', 'n']
[x for x in range(10) if x % 2 == 0]     # [0, 2, 4, 6, 8]
[me[x] for x in range(10) if x % 2 == 0] # ['K', 'v', 'n', 'Y', 'n']
</code></pre>
<p>But all that syntactic sugar aside, the biggest feature of Python that I appreciate after years of coding PHP is its sensible system of modules and packages for organizing code into multiple files. In PHP, when one script <code>include</code>s (or <code>require</code>s) another script, it runs the risk of having its own variable, function, and class names clobbered by the script that it is including.</p>
<div id="adz" class="vertical"></div><p>Python automatically sets up a separate namespace for each file (called a module) that your program imports, so that naming conflicts are naturally avoided. This frees you up to choose shorter, more natural names (especially for classes in big projects), while at the same time forcing you to give more thought to the structure of your code.</p>
<h2>Where to Begin?</h2>
<p>There is little sense in learning a new language unless you have a reason to use it. For the past couple of years, that reason for many web developers has been <a href="http://www.djangoproject.com/">Django</a>. The best place to start, therefore, is probably SitePoint’s <a href="http://www.sitepoint.com/article/build-to-do-list-30-minutes">Django Djumpstart</a> article.</p>
<p>More recently, a lot of developers are taking renewed interest in Python because it is the development language for <a href="http://www.sitepoint.com/blogs/2008/04/08/google-reveals-app-engine">Google App Engine</a>. It’s worth having a look at App Engine before diving into Python.</p>
<p>Speaking of diving into Python, Mark Pilgrim’s book <a href="http://www.diveintopython.org/">Dive Into Python</a> is available to read online for free, and is a great way to learn the language from scratch. If you’re after something with more depth and detail, the <a href="http://docs.python.org/tut/tut.html">official Python Tutorial</a> is a rewarding read, if a bit dry. It’s also significantly more up to date.</p>
<script src="http://ads.aws.sitepoint.com/adjs.php?region=136&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2008/05/09/a-php-guy%e2%80%99s-look-at-python/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.612 seconds -->
<!-- Cached page served by WP-Cache -->
