<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cowburn &#187; PHP</title>
	<atom:link href="http://cowburn.info/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://cowburn.info</link>
	<description>Online storage depot for Peter Cowburn</description>
	<lastBuildDate>Thu, 22 Jul 2010 17:51:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>PHP 5.3.3 released!</title>
		<link>http://cowburn.info/2010/07/22/php-5-3-3-released/</link>
		<comments>http://cowburn.info/2010/07/22/php-5-3-3-released/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 10:47:14 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[php.net]]></category>
		<category><![CDATA[php5.3.3]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=440</guid>
		<description><![CDATA[Today, the latest version of PHP 5.3 (as well as the latest in the 5.2 branch, 5.2.14) has been released to the masses. The official announcement can found in the news archive with finer details in the changelog. A select few of the changes, of particular interest to me, are outlined below. Added support for [...]]]></description>
			<content:encoded><![CDATA[<p>Today, the latest version of PHP 5.3 (as well as the latest in the 5.2 branch, 5.2.14) has been released to the masses. The official announcement can found in the <a href="http://www.php.net/archive/2010.php#id2010-07-22-2" title="PHP: News Archive - 2010">news archive</a> with finer details in the <a href="http://www.php.net/ChangeLog-5.php#5.3.3" title="PHP 5.3.3 Changelog">changelog</a>.  A select few of the changes, of particular interest to me, are outlined below.<span id="more-440"></span></p>
<blockquote cite="http://www.php.net/ChangeLog-5.php#5.3.3">
<ul>
<li>Added support for copy to/from array/file for pdo_pgsql extension. (Denis Gasparin, Ilia)</li>
<li>Changed namespaced classes so that the ctor can only be named __construct now. (Stas)</li>
<li>Fixed bug #51590 (JSON_ERROR_UTF8 is undefined). (Felipe)</li>
<li>Fixed bug #49576 (FILTER_VALIDATE_EMAIL filter needs updating) (Rasmus)</li>
<li>Fixed bug #48361 (SplFileInfo::getPathInfo should return the parent dir). (Etienne)</li>
<li>And well over a hundred other bug and security fixes</li>
</ul>
</blockquote>
<p>The change to constructors in namespaced classes has already been the subject of &#8220;why doesn&#8217;t this work&#8221; questions. With 5.3.3 you <em>cannot</em> define class constructors like below.</p>
<pre><code lang="php">
namespace Foo {
    class Bar {
        function Bar() {
            // constructing \Foo\Bar class
        }
    }
}
</code></pre>
<p>Instead, you need to be using the right and proper <code lang="php">__construct</code> method. Note that this only applies to namespaced classes, so old code can continue (if they like) using the class-name-based constructor. This really only affects the (few?) folks who might be making use of, or adapting their old code to use, namespaces yet still keeping the old style of naming the constructor method.</p>
<p>Go, play!</p>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2010/07/22/php-5-3-3-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Happy 15th, PHP</title>
		<link>http://cowburn.info/2010/06/08/happy-15th-php/</link>
		<comments>http://cowburn.info/2010/06/08/happy-15th-php/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 07:58:57 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[birthday]]></category>
		<category><![CDATA[php.net]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=429</guid>
		<description><![CDATA[Today marks the 15th birthday* of PHP. I did have a nice, long post planned but lost it during the drafting process (maybe I should use WordPress which auto-saves?). So, in a new concise train of thought I&#8217;ll leave the rambling to other folks in the PHP community who will (hopefully) be providing their reflections [...]]]></description>
			<content:encoded><![CDATA[<p>Today marks the 15th birthday<a href="#fn1">*</a> of PHP. I did have a nice, long post planned but lost it during the drafting process (maybe I should use WordPress which auto-saves?).  So, in a new concise train of thought I&#8217;ll leave the rambling to other folks in the PHP community who will (hopefully) be providing their reflections over the last decade and a half. I&#8217;ll amend links to this post for those who don&#8217;t want to search around for birthday wishes.</p>
<p>Happy birthday, PHP! <img src='http://cowburn.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> <span id="more-429"></span></p>
<p class="aside" id="fn1">* See <a href="http://groups.google.com/group/comp.infosystems.www.authoring.cgi/browse_thread/thread/3f2276e6067acfb0/cc7d43454d64d133" title="Announce: Personal Home Page Tools (PHP Tools) Options">Announce: Personal Home Page Tools (PHP Tools) Options</a> (8th June 1995)</p>
<h3>Elsewhere</h3>
<ul>
<li><a href="http://schlueters.de/blog/archives/136-15-years-of-PHP.html" title="15 years of PHP">15 years of PHP</a> from Johannes Schlüter</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2010/06/08/happy-15th-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Glob Patterns for File Matching in PHP</title>
		<link>http://cowburn.info/2010/04/30/glob-patterns/</link>
		<comments>http://cowburn.info/2010/04/30/glob-patterns/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 18:16:46 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[glob]]></category>
		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=390</guid>
		<description><![CDATA[This article has been on the cards for a while now with recent articles elsewhere1,2 prompting me to get this finished and up on the blog. The focus here is on the wildcard patterns that can be used with glob. Intro to the glob function To quote the succinct description on the PHP manual page [...]]]></description>
			<content:encoded><![CDATA[<p>This article has been on the cards for a while now with recent articles elsewhere<sup><a href="#fn1" title="php|architect - Putting glob() to the test">1</a>,<a href="#fn2" title="Nettuts+ - Loop Through Folders with PHP’s Glob()">2</a></sup> prompting me to get this finished and up on the blog. The focus here is on the wildcard patterns that can be used with <code>glob</code>.<span id="more-390"></span>  </p>
<h3>Intro to the <code>glob</code> function</h3>
<p>To quote the succinct description on the <a href="http://php.net/glob" title="PHP: Glob">PHP manual page</a> for <code>glob</code>:</p>
<blockquote cite="http://php.net/glob" datetime="2010-04-30T16:20:00Z"><p>The <code>glob()</code> function searches for all the pathnames matching <var>pattern</var> according to the rules used by the libc <code>glob()</code> function, which is similar to the rules used by common shells.<br />
<cite>http://php.net/glob</cite></p></blockquote>
<h3>What&#8217;s in a pattern?</h3>
<p>Most people who have already encountered <code>glob</code> know to make use of the <code>*</code> metacharacter to match <em>some characters</em>, and those digging a little deeper often discover that discrete alternatives can be globbed with braces (e.g. <code>image.{gif,jpg,png}</code>). However, there are more special characters and sequences that can be used to be more (or less, if we want) specific about what to find. </p>
<p class="small"><em>Aside</em>: please <strong>do not</strong> make the mistake of thinking that glob patterns are <em>regular expressions</em>, they&#8217;re just not. If you do want to use regular expressions to find paths/files then you are invited to use SPL&#8217;s <a href="http://php.net/regexiterator" title="PHP: SPL RegexIterator">RegexIterator</a>, which allows filtering of an Iterator based on a PCRE regex, in conjunction with a DirectoryIterator or FilesystemIterator (there are recursive flavours of the Regex- and DirectoryIterator if you need to delve into folders). For those SPL-ly inclined, also note the <a href="http://php.net/globiterator" title="PHP: SPL GlobIterator">GlobIterator</a> which combines the goodness of globbing with iteration. If that made entirely <em>no sense</em>, please read on! Globs are much less verbose…</p>
<p>So, here are the special doohickeys (technical term!) that we can use with <code>glob</code>:</p>
<dl>
<dt><code>*</code> (an asterisk)</dt>
<dd>Matches zero of more characters.</dd>
<dt><code>?</code></dt>
<dd>Matches exactly any one character.</dd>
<dt><code>[...]</code></dt>
<dd>Matches one character from a group.  A group can be a list of characters, e.g. <code>[afkp]</code>, or a range of characters, e.g. <code>[a-g]</code> which is the same as <code>[abcdefg]</code>.</dd>
<dt><code>[!...]</code></dt>
<dd>Matches any single character not in the group. <code>[!a-zA-Z0-9]</code> matches any character that is not alphanumeric.</dd>
<dt><code>\</code></dt>
<dd>Escapes the next character. For special characters, this causes them to not be treated as special. For example, <code>\[</code> matches a literal <samp>[</samp>. If <var>flags</var> includes <var>GLOB_NOESCAPE</var>, this quoting is disabled and <code>\</code> is handled as a simple character. </dd>
</dl>
<h3>Globbingly good glob examples</h3>
<p>Here are a few examples of what globs might look like alongside a brief description of the intended behaviour: if you have any suggestions please do make them in the comments as I'm running short on inspiration! </p>
<table class="light">
<thead>
<tr style="width:20%">
<th>pattern</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>*.txt</code></td>
<td>Get directory contents which have the extension of <samp>.txt</samp> (Note: a file could be named simply <samp>.txt</samp>!).</td>
</tr>
<tr>
<td><code>??</code></td>
<td>Get directory contents with names <em>exactly</em> two characters in length.</td>
</tr>
<tr>
<td><code>??*</code></td>
<td>Get directory contents with names <em>at least</em> two characters in length.</td>
</tr>
<tr>
<td><code>g?*</code></td>
<td>Get directory contents with names at least two characters in length and starting with the letter <samp>g</samp></td>
</tr>
<tr>
<td><code>*.{jpg,gif,png}</code></td>
<td>Get directory contents with an extension of <samp>.jpg</samp>, <samp>.gif</samp> or <samp>.png</samp>. Remember to use the <var>GLOB_BRACE</var> flag.</td>
</tr>
<tr>
<td><code>DN?????.dat</code></td>
<td>Get directory contents which start with the letters <samp>DN</samp>, followed by five characters, with an extension of <samp>.dat</samp>.</td>
</tr>
<tr>
<td><code>DN[0-9][0-9][0-9][0-9][0-9].dat</code></td>
<td>Get directory contents which start with the letters <samp>DN</samp>, followed by five <em>digits</em>, with an extension of <samp>.dat</samp>.</td>
</tr>
<tr>
<td><code>[!aeiou]*</code></td>
<td>Get directory contents which do not start with a vowel letter.</td>
</tr>
<tr>
<td><code>[!a-d]*</code></td>
<td>Get directory contents which do not start with <samp>a</samp>, <samp>b</samp>, <samp>c</samp> or <samp>d</samp>.</td>
</tr>
<tr>
<td><code>*\[[0-9]\].*</code></td>
<td>Get directory contents whose basename ends with a single digit enclosed in square braces. If <var>GLOB_NOESCAPE</var> is used, a single digit enclosed in <samp>\[</samp> and <samp>\]</samp> which would be a pretty weird name.</td>
</tr>
<tr>
<td><code>subdir/img*/th_?*</code></td>
<td>Get directory contents whose name starts with <samp>th_</samp> (with at least one character after that) within directories whose names start with <samp>img</samp> in the <samp>subdir</samp> directory.</td>
</tr>
</tbody>
</table>
<p>Well there we go, I&#8217;ve said what I came here to say so all that remains to be done is give some link love to those  two recent articles that prompted me to dust off this draft and click the &#8220;publish&#8221; button. </p>
<h6>With thanks</h6>
<ol>
<li id="fn1">php|architect&#8217;s <a href="http://www.phparch.com/2010/04/28/putting-glob-to-the-test/" title="Putting glob() to the test">Putting glob() to the test</a> focuses on execution times of <code>glob</code> and other techniques of getting a list of files.</li>
<li id="fn2">Marcus Schumann&#8217;s <a href="http://net.tutsplus.com/tutorials/php/quick-tip-loop-through-folders-with-phps-glob/" title="Loop Through Folders with PHP’s Glob()">Loop Through Folders with PHP’s Glob()</a> article on Nettuts+ gives a brief introduction for those who might not have been introduced to <code>glob</code>.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2010/04/30/glob-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using PHP Functions in XPath Expressions</title>
		<link>http://cowburn.info/2009/10/23/php-funcs-xpath/</link>
		<comments>http://cowburn.info/2009/10/23/php-funcs-xpath/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 20:41:13 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[XPath]]></category>
		<category><![CDATA[DOM]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=336</guid>
		<description><![CDATA[Disclaimer: this article expects familiarity with using the DOM1 extension and XPath2 expressions. The (currently undocumentednow documented3) DOMXPath::registerPHPFunctions method is available as of PHP 5.3.0 (it was added to the code base back in December 2006) and allows the use of PHP functions (and static methods) within XPath queries to complement the normal set of [...]]]></description>
			<content:encoded><![CDATA[<p>Disclaimer: this article expects familiarity with using the DOM<sup><a href="#fn1" title="footnote 1">1</a></sup> extension and XPath<sup><a href="#fn2" title="footnote 2">2</a></sup> expressions. </p>
<p>The (<del datetime="2009-10-26T14:15:44Z">currently undocumented</del><ins datetime="2009-10-26T14:15:44Z">now documented</ins><sup><a href="#fn3" title="footnote 3">3</a></sup>) <code>DOMXPath::registerPHPFunctions</code> method is available as of PHP 5.3.0 (it was added to the code base back in December 2006) and allows the use of PHP functions (and static methods) within XPath queries to complement the normal set of XPath functions<sup><a href="#fn2" title="footnote 2">2</a></sup>.<span id="more-336"></span></p>
<h3>Description</h3>
<p><code>void <strong>DOMXPath::registerPHPFunctions</strong>  ([ string|array <var>$restrict</var>] )</code></p>
<p>Enables the use of PHP functions as XPath functions.</p>
<h3>Parameters</h3>
<dl>
<dt>restrict</dt>
<dd>
Use this parameter to only allow certain functions to be called from XPath; it can be either a string (a function name) or an array of function names.
</dd>
</dl>
<h3>Return Values</h3>
<p>No value is returned.</p>
<h3>Examples</h3>
<p><strong>Note:</strong> The following examples load a sample XML document called <kbd>book.xml</kbd> with the following contents:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" ><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;books<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>PHP Basics<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Jim Smith<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Jane Smith<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>PHP Secrets<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Jenny Smythe<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>XML basics<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Joe Black<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/books<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3 id="ex1">Example #1: Call a PHP function in XPath with <code>php:functionString</code></h3>
<p>This example demonstrates the basic use of <code>DOMXPath::registerPHPFunctions</code> by replicating the <code>substring</code> XPath function. The first thing that needs to be done is to register the <samp>php</samp> namespace with the associated URI <samp>http://php.net/xpath</samp>. Don&#8217;t question it, it just needs to be done!</p>
<p>Next, we call <code>DOMXPath::registerPHPFunctions</code> on our object. If no arguments are used, as in this example, then the range of functions allowed to be called is not restricted—you can call any<sup><a href="#fn4" title="footnote 4">4</a></sup> function from XPath. I would always advise restricting the functions which can be called, see <a href="#ex2" title="Example 2">example 2</a>.</p>
<p>Within our XPath query, we use <code>php:functionString</code> which allows us to name a function and provide some parameters (or indeed, no parameters) to be passed to that function. There are two flavours which can be used here: <code>php:functionString</code> which passes an XML node/attribute as a string and <code>php:function</code> (see <a href="#ex3" title="Example 3">example 3</a>) which passes an array of XML node/attribute objects (in <code>DOMElement</code> / <code>DOMAttr</code> / etc. form) to the function .  In this example the PHP function <a href="http://php.net/substr"><code>substr</code></a> is called, passing along the book&#8217;s title (in string form), an offset of <samp>0</samp> and length of <samp>3</samp>. This returns the first 3 characters of the book&#8217;s title which is then compared to the string <kbd>PHP</kbd> in order to filter our list of books down to those having titles starting with <kbd>PHP</kbd>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #339933;">;</span>
<span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$xpath</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMXPath<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dom</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Register the php: namespace (required)</span>
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerNamespace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;http://php.net/xpath&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Register PHP functions (no restrictions)</span>
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerPHPFunctions</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Call substr function on the book title</span>
<span style="color: #000088;">$nodes</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'//book[php:functionString(&quot;substr&quot;, title, 0, 3) = &quot;PHP&quot;]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Found <span style="color: #006699; font-weight: bold;">{$nodes-&gt;length}</span> books starting with 'PHP':<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$nodes</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$title</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$author</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;author&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$title</span> by <span style="color: #006699; font-weight: bold;">$author</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The example will output something like:</p>

<div class="wp_syntax"><div class="code"><pre class="plain" >Found 2 books starting with 'PHP':
PHP Basics by Jim Smith
PHP Secrets by Jenny Smythe</pre></div></div>

<h3 id="ex2">Example #2: Restricting the functions available to XPath</h3>
<p>To restrict the functions made available to XPath, provide either a string containing the name of the single function that you wish to allow or an array of strings containing function names as the <var>restrict</var> parameter (note: static methods can also be used, e.g. &#8220;<samp>Classname::method</samp>&#8220;).  If functions were added with <var>restrict</var> and a function is called in XPath which is not one of them, an <var>E_WARNING</var> will be raised stating <samp>Not allowed to call handler &#8216;<var>function</var>()&#8217;</samp> (where <var>function</var> is the name of the function that cannot be called).</p>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #339933;">;</span>
<span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$xpath</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMXPath<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dom</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Register the php: namespace (required)</span>
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerNamespace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;http://php.net/xpath&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Register PHP functions (no restrictions)</span>
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerPHPFunctions</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;strtoupper&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get first book's title in uppercase</span>
<span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">evaluate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'php:functionString(&quot;strtoupper&quot;, //book[1]/title)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$title</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Try a function not in our restrictions list</span>
<span style="color: #000088;">$fail</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">evaluate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'php:functionString(&quot;strtolower&quot;, //book[1]/title)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The example will output something like:</p>

<div class="wp_syntax"><div class="code"><pre class="plain" >PHP BASICS&lt;br /&gt;
&lt;b&gt;Warning&lt;/b&gt;:  DOMXPath::evaluate() [&lt;a href='domxpath.evaluate'&gt;domxpath.evaluate&lt;/a&gt;]: Not allowed to call handler 'strtolower()'. in &lt;b&gt;example2.php&lt;/b&gt; on line &lt;b&gt;18&lt;/b&gt;&lt;br /&gt;</pre></div></div>

<h3 id="ex3">Example #3: Passing DOM objects using <code>php:function</code></h3>
<p>Up to now, the examples have both used <code>php:functionString</code>. As mentioned above, instead of passing a string value to the PHP function it is possible to pass along an array of DOM* objects to manipulate them as you please by using <code>php:function</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #339933;">;</span>
<span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$xpath</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMXPath<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dom</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Register the php: namespace (required)</span>
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerNamespace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;http://php.net/xpath&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Register PHP functions (no restrictions)</span>
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerPHPFunctions</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;example3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> example3<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nodes</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Return true if more than one author</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nodes</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// Filter books with multiple authors</span>
<span style="color: #000088;">$books</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'//book[php:function(&quot;example3&quot;, author)]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Books with multiple authors:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$books</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$book</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$book</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The example will output something like:</p>

<div class="wp_syntax"><div class="code"><pre class="plain" >Books with multiple authors:
PHP Basics</pre></div></div>

<h3>Summary</h3>
<p>Just to quickly summarise everything, here is a quick run-down. In PHP, make sure to register the <kbd>php</kbd> namespace (with the URI <kbd>http://php.net/xpath</kbd>) and then register your PHP functions (whether core, extensions or user-defined) or static methods with <code>DOMXPath::registerPHPFunctions</code>. In XPath, use <code>php:functionString</code> or <code>php:function</code> to call the PHP function.</p>
<p>If you have made use of this feature, or want to know more, then do feel free to comment. Thanks for reading.</p>
<h4>Footnotes</h4>
<ol>
<li id="fn1"><a href="http://php.net/dom" title="PHP: DOM">http://php.net/dom</a></li>
<li id="fn2"><a href="http://schlitt.info/opensource/blog/0704_xpath.html" title="XPath overview">http://schlitt.info/opensource/blog/0704_xpath.html</a>
<li id="fn3">The documentation page, <del datetime="2009-10-26T14:15:44Z">when it gets written, will be</del><ins datetime="2009-10-26T14:15:44Z">is</ins> available at <a href="http://php.net/domxpath.registerphpfunctions" title="PHP: DOMXPath::registerPHPFunctions">http://php.net/domxpath.registerphpfunctions</a></li>
<li id="fn4">In truth, some functions are not suitable such as those that return non-scalar values (and cannot be cast to one) which XPath will not understand.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/10/23/php-funcs-xpath/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The InfiniteIterator in PHP</title>
		<link>http://cowburn.info/2009/09/25/infiniteiterator/</link>
		<comments>http://cowburn.info/2009/09/25/infiniteiterator/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 12:49:28 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[infiniteiterator]]></category>
		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=316</guid>
		<description><![CDATA[In an article back in July (Anonymous Functions and Closures (as of PHP 5.3)), I gave an example of looping over a series of values repeatedly. Whilst that example does the job (and introduces the concept of closures) it&#8217;s hardly the most convenient method of repeatedly iterating over a series of values. Introducing the InfiniteIterator [...]]]></description>
			<content:encoded><![CDATA[<p>In an article back in July (<a href="http://cowburn.info/2009/07/08/anonymous-closures/" title="Read Anonymous Functions and Closures (as of PHP 5.3)">Anonymous Functions and Closures (as of PHP 5.3)</a>), I gave <a href="http://cowburn.info/2009/07/08/anonymous-closures/#fig5" title="See example: Cycling with Closures">an example</a> of looping over a series of values repeatedly.  Whilst that example does the job (and introduces the concept of closures) it&#8217;s hardly the most convenient method of repeatedly iterating over a series of values.  Introducing the <a href="http://php.net/InfiniteIterator" title="PHP Manual: InfiniteIterator"><code>InfiniteIterator</code></a> which is part of the <a href="http://php.net/spl" title="Standard PHP Library">Standard PHP Library</a> (SPL).<span id="more-316"></span></p>
<p>On the plus side, for those who have yet to take the plunge into using PHP 5.3, this iterator has been part of the core of PHP as of 5.1.0 so there is a much greater chance of actually being able to use it right now.  Bear in mind that the SPL is supposed to work together with its component parts so if <a href="http://php.net/iterator" title="PHP: Object Iteration">iterators</a> are a foreign concept, some of this post might be a little unclear but it is not the purpose of this post to outline iterators in PHP.</p>
<p>As eluded to above, the <code>InfiniteIterator</code> comes in useful when you have an existing <code>Iterator</code> (anything that implements that interface; a directory listing (<code>DirectoryIterator</code>), a file (<code>SplFileObject</code>), an array (with <code>ArrayIterator</code>), and so on) and wish to iterate over its contents again and again. </p>
<h3>An <code>InfiniteIterator</code> Example</h3>
<p>Here is a basic example that I wrote for the <a href="http://php.net/InfiniteIterator.construct" title="PHP Manual: InfiniteIterator::__construct">documentation</a>. Note that a <a href="http://php.net/LimitIterator" title="The LimitIterator class"><code>LimitIterator</code></a> is used to restrict the values which are iterated over (otherwise the loop would go on forever!).</p>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$arrayit</span>  <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayIterator<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cat'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'dog'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$infinite</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> InfiniteIterator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$arrayit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$limit</span>    <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LimitIterator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$infinite</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$limit</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$value</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This example outputs something along the lines of:</p>

<div class="wp_syntax"><div class="code"><pre class="plain" >cat
dog
cat
dog
cat
dog
cat</pre></div></div>

<p>Can you think of any more useful instances where the <code>InfiniteIterator</code> might come in useful?</p>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/09/25/infiniteiterator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What Firefly/Serenity Can Teach Us About PHP (external)</title>
		<link>http://cowburn.info/2009/08/11/firefly-php/</link>
		<comments>http://cowburn.info/2009/08/11/firefly-php/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 14:36:12 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[external]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=303</guid>
		<description><![CDATA[This update is just really linking through to What Firefly/Serenity Can Teach Us About PHP on PHP Developer where the author makes some comparison between the awesome sci-fi series Firefly and PHP. Worth a few minutes at least: go read. Here&#8217;s the summary Just because it’s not the latest and greatest version, it doesn’t mean [...]]]></description>
			<content:encoded><![CDATA[<p>This update is just really linking through to <a href="http://blog.phpdeveloper.org/?p=158">What Firefly/Serenity Can Teach Us About PHP</a> on <a href="http://blog.phpdeveloper.org/">PHP Developer</a> where the author makes some comparison between the awesome sci-fi series Firefly and PHP. Worth a few minutes at least: <a href="http://blog.phpdeveloper.org/?p=158">go read</a>.<span id="more-303"></span></p>
<p>Here&#8217;s the summary</p>
<ul>
<li>Just because it’s not the latest and greatest version, it doesn’t mean it’s not good</li>
<li>Just because you don’t understand something, it doesn’t mean you shouldn’t embrace it</li>
<li>There’s nothing stronger than a bond between those who care</li>
<li>It takes all kinds to make the &#8216;verse go &#8217;round</li>
<li>Use only what you need</li>
<li>A good team is nothing without good leaders</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/08/11/firefly-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Anonymous Functions and Closures (as of PHP 5.3)</title>
		<link>http://cowburn.info/2009/07/08/anonymous-closures/</link>
		<comments>http://cowburn.info/2009/07/08/anonymous-closures/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 14:41:38 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[anonymous functions]]></category>
		<category><![CDATA[closures]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=234</guid>
		<description><![CDATA[There has been much discussion, especially of late considering the release of PHP 5.3, on the subject of anonymous functions and closures (both terms appear to be used interchangeably in the documentation). They are not the same, though many folks are introduced to them at the same time. Anonymous Functions Put plainly, an anonymous function [...]]]></description>
			<content:encoded><![CDATA[<p>There has been much discussion, especially of late considering the release of PHP 5.3, on the subject of anonymous functions and closures (both terms appear to be used interchangeably in the <a href="http://php.net/closures" title="Closures in the PHP Manual">documentation</a>).  They are not the same, though many folks are introduced to them at the same time.<span id="more-234"></span></p>
<h3>Anonymous Functions</h3>
<p>Put plainly, an anonymous function is a function without a name. That&#8217;s all, there&#8217;s nothing magic or particularly technical about it.  There are a few ways to create an anonymous function in PHP; up until PHP 5.3 came around the usual way was to use <a href="http://php.net/create_function" title="create_function documentation on php.net"><code>create_function</code></a> (see <a href="#fig1" title="View figure 1: Anonymous function with create_function">fig. 1</a>) which takes a couple of arguments as strings and spits out a function, in PHP 5.3 we&#8217;re allowed to create anonymous functions looking much more like normal function definitions (see <a href="#fig2" title="View figure 2: Anonymous function in PHP 5.3">fig. 2</a>).</p>
<h4 id="fig1">Fig. 1: Anonymous function with create_function</h4>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$square</span> <span style="color: #339933;">=</span> <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'$x'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'return $x*$x;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$square</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 4</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h4 id="fig2">Fig 2: Anonymous function as of PHP 5.3</h4>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$square</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$square</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 4</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>I hope you&#8217;ll agree that the latter is a much nicer way (visually) of defining an anonymous function, quite apart from the more logical and performance-related (see <a href="http://fabien.potencier.org/article/17/on-php-5-3-lambda-functions-and-closures#comments" title="Comments on 'On PHP 5.3, Lambda Functions, and Closures'">comment #4</a>) aspects of using it.</p>
<h3>Closures</h3>
<p>A closure is a (anonymous) function that is aware of its context. For all intents and purposes, think of them being anonymous functions which know about some variables which weren&#8217;t defined within them. In geeky language, they are functions which close over (hence &#8220;closure&#8221;) free variables, the latter being variables which are not local nor arguments to the function.  It is easier to see with an example, I think.</p>
<h4 id="fig3">Without Closure</h4>
<p>Lets say that we need a function to add three to a number (overly basic, I know). That&#8217;s easy.</p>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$add_3</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$add_3</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 13</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now if we wanted another function to behave similarly but instead it should add 4 to the supplied number, we could just as easily create a new anonymous function to do the job.</p>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$add_4</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$add_4</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 14</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>If, however, you wanted to do that for a series of different additions then things could get very repetitive, very quickly!  If only we could dynamically create an anonymous function to do what we wanted: enter the closure.</p>
<h4 id="fig4">With Closure</h4>
<p>We can define an anonymous function (it needn&#8217;t be anonymous but lets keep on theme) which returns anonymous function based on arguments supplied to it. Remember our definition of a closure above, specifically making use of &#8216;free&#8217; variables, that is what our <code>$value_to_add</code> is in the example below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$add</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value_to_add</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> use <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value_to_add</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$value_to_add</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$add_3</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$add_4</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$add_3</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 13</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$add_4</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 14</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h3>A more practical example</h3>
<p>As fun as silly adding examples are, there will be undoubtedly someone who has been reading everything above (good job getting this far!) and cannot see a practical use for all of this. Since <a href="http://www.letour.fr/" title="Site officiel du tour de France">le Tour de France</a> is on, lets have a play on words and have an example on cycling (over a series of values).</p>
<h4 id="fig5">Cycling with Closures</h4>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Cycle factory: takes a series of arguments</span>
<span style="color: #666666; font-style: italic;">// for the closure to cycle over.</span>
<span style="color: #000000; font-weight: bold;">function</span> cycle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">func_get_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Create closure using the supplied list of arguments</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> use <span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Get current item</span>
        <span style="color: #000088;">$current</span> <span style="color: #339933;">=</span> <span style="color: #990000;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Rewind if at the end of the list</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$current</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #990000;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$current</span> <span style="color: #339933;">=</span> <span style="color: #990000;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">// Return the current item</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$current</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'value'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create our list of CSS classes to cycle over</span>
<span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> cycle<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'odd'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'even'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// List of adjectives, why not</span>
<span style="color: #000088;">$taste</span> <span style="color: #339933;">=</span> cycle<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'vile'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'yummy'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'delicious'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Our list of items to be formatted into a pretty HTML list</span>
<span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'apple'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'banana'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cherry'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'date'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'elderberry'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'figs'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;ul&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;li class=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$item</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> are <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$taste</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/li&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/ul&gt;</pre></div></div>

<p>That example would provide the following output:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" ><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;odd&quot;</span>&gt;</span>apples are vile<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;even&quot;</span>&gt;</span>bananas are yummy<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;odd&quot;</span>&gt;</span>cherries are delicious<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;even&quot;</span>&gt;</span>dates are vile<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;odd&quot;</span>&gt;</span>elderberries are yummy<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;even&quot;</span>&gt;</span>figs are delicious<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span></pre></div></div>

<p>Well, it&#8217;s a little bit more practical anyway!  Have you been playing with closures and/or anonymous functions too?</p>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/07/08/anonymous-closures/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP 5.3.0 Released</title>
		<link>http://cowburn.info/2009/06/30/php-530/</link>
		<comments>http://cowburn.info/2009/06/30/php-530/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 13:12:45 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[php.net]]></category>
		<category><![CDATA[php5.3.0]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=224</guid>
		<description><![CDATA[Today the latest stable version of PHP was announced, PHP 5.3.0. Go download and play with the new features available such as namespaces, LSB, anonymous functions and closures along with many more bug fixes, additions and improvements. My favourite addition at the moment is the improvements made to the DateTime extension with new classes such [...]]]></description>
			<content:encoded><![CDATA[<p>Today the latest stable version of PHP was announced, <a href="http://php.net/releases/5_3_0.php" rel="external">PHP 5.3.0</a>.  </p>
<p>Go download and play with the new features available such as <a href="http://php.net/namespaces">namespaces</a>, <a href="http://php.net/lsb" title="late static binding">LSB</a>, <a href="http://php.net/closures">anonymous functions and closures</a> along with many more bug fixes, additions and improvements.<span id="more-224"></span></p>
<p>My favourite addition at the moment is the improvements made to the <a href="http://php.net/datetime">DateTime</a> extension with new classes such as <a href="http://php.net/dateperiod">DatePeriod</a> (for iterating over dates/times) and <a href="http://php.net/dateinterval">DateInterval</a> and additions to the DateTime class such as <a href="http://php.net/datetime.createFromFormat">DateTime::createFromFormat</a> and <a href="http://php.net/datetime.add">DateTime::add</a> (and sub).</p>
<p>The anonymous functions will also be very useful where one might previously use <a href="http://php.net/create_function">create_function</a> (or a normal non-anonymous function) in particular for callbacks to other functions/methods.</p>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">preg_replace_callback</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/[A-Z]/'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">str_rot13</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Uello Jorld!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>So, download it and have an explore. <img src='http://cowburn.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/06/30/php-530/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parsing Dates with DateTime</title>
		<link>http://cowburn.info/2009/06/14/parsing-dates-with-datetime/</link>
		<comments>http://cowburn.info/2009/06/14/parsing-dates-with-datetime/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 11:39:26 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[strtotime]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=182</guid>
		<description><![CDATA[This post comes on the back of a number of forum posts I&#8217;ve seen floating around where the Original Poster (OP) asks a question along the lines of, &#8220;I have a date in dd/mm/yyy format, how can I work with it?&#8221; The problem is that the most common way of parsing strings into a more [...]]]></description>
			<content:encoded><![CDATA[<p>This post comes on the back of a number of forum posts I&#8217;ve seen floating around where the Original Poster (OP) asks a question along the lines of, &#8220;<q>I have a date in dd/mm/yyy format, how can I work with it?</q>&#8221;</p>
<p>The problem is that the most common way of parsing strings into a more usable format (Unix timestamp) is to use <code>strtotime()</code>. However, this function does not like <samp>dd/mm/yyyy</samp> formatted strings and will at best parse the string as <samp>mm/dd/yyyy</samp> and at worst return <var>FALSE</var> (if it could not parse the date string as mm/dd/yyyy).<span id="more-182"></span></p>
<h3>Quick workaround</h3>
<p>One quick thing that works, and I&#8217;ve advocated in forum posts before, is to provide a date string in the format <samp>dd-mm-yyyy</samp>. Note that the date parts are in the same order, just the separator has changed from a forward slash (<code>/</code>) to a hyphen (<code>-</code>).  A date formatted in this way will be happily parsed by <code>strtotime</code>. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #990000;">date_default_timezone_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'UTC'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'14/06/2009'</span><span style="color: #339933;">;</span>                 <span style="color: #666666; font-style: italic;">// dd/mm/yyyy</span>
<span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'-'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// dd-mm-yyyy</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'r'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Sun, 14 Jun 2009 00:00:00 +0000</span></pre></div></div>

<h3>Using DateTime</h3>
<p>As of PHP 5.2.0 (and experimentally in 5.1.x) we have also had the ability to play with dates and times using the built-in <code>DateTime</code> extension <span class="aside">(Aside: the <code>date_default_timezone_set</code> function used above comes from this extension)</span>. To repeat the above but in <code>DateTime</code> form:</p>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'14/06/2009'</span><span style="color: #339933;">;</span>                 <span style="color: #666666; font-style: italic;">// dd/mm/yyyy</span>
<span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'-'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// dd-mm-yyyy</span>
<span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">new</span> DateTimeZone<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'UTC'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$date</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Sun, 14 Jun 2009 00:00:00 +0000</span></pre></div></div>

<h3>Help Coming!</h3>
<p>I&#8217;m sure that you&#8217;ll agree, the above is hardly a perfect solution to the problem (but is a sufficient workaround for now).  However help is at hand.  As of <strong>PHP 5.3.0</strong> we will be able to make use of a new static method on the <code>DateTime</code> class.  This method is <code>DateTime::createFromFormat</code> which requires two arguments; a string specifying the format which the date string has (as used by the <code>date</code> function), and the date string itself (there is also a third, optional, argument for the timezone to be used).</p>
<p>An example of its use, analogous to the snippets above, would be:</p>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> DateTime<span style="color: #339933;">::</span><span style="color: #004000;">createFromFormat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'14/06/2009'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">new</span> DateTimeZone<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'UTC'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$date</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setTime</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$date</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Sun, 14 Jun 2009 00:00:00 +0000</span></pre></div></div>

<p><span class="aside">Aside: For my installation (PHP 5.3.0RC3) this method does not parse the date string as midnight, as the other code snippets do, but will use current time! This is why the <code>setTime</code> method (available as of 5.2.0) is used, to force the time to be midnight (arguments are <var>hour</var>, <var>minute</var>, <var>second</var>). <em>Edit:</em> One could instead use the <code>!</code> within the format string like <code>!d/m/Y</code>, since the time portion is missing it will substitute the Unix epoch&#8217;s time (midnight) in its place. See the manual for details of <code>!</code>.</span></p>
<p>Just to wrap things up, the <code>DateTime::createFromFormat</code> should help considerably towards parsing previously ambiguous date strings into forms that can be manipulated and at least will get around the string manipulation being offered as a solution right now. If you&#8217;re still stuck playing around with the date string, at least  you know that <samp>dd-mm-yyyy</samp> will be parsed as you want.</p>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/06/14/parsing-dates-with-datetime/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Joined the PHP Documentation Team</title>
		<link>http://cowburn.info/2009/06/12/php-doc/</link>
		<comments>http://cowburn.info/2009/06/12/php-doc/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 14:43:46 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=173</guid>
		<description><![CDATA[This is just a very short update to announce quietly that I recently joined the team of folks working on the PHP documentation. It&#8217;s another step towards giving a little something back to the PHP community by adding to and updating the constantly evolving docs which have helped me along for so many years. Incidentally, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://cowburn.info/wp-content/uploads/2009/06/php-med-trans1.png" alt="PHP logo" title="PHP logo" width="95" height="51" class="alignleft size-full wp-image-175" /> This is just a very short update to announce quietly that I recently joined the team of folks working on the <a href="http://php.net/docs" title="PHP Documentation">PHP documentation</a>.  It&#8217;s another step towards giving a little something back to the PHP community by adding to and updating the constantly evolving docs which have helped me along for so many years.  Incidentally, my first commit was too large to be sent out to the mailing list tracking the changes to the documentation. Oops! </p>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/06/12/php-doc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
