<?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>David Otton &#187; programming</title>
	<atom:link href="http://www.otton.org/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.otton.org</link>
	<description>Look! Bunnies!</description>
	<lastBuildDate>Wed, 06 Jan 2010 14:32:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>More switch() Abuse &#8211; Switching on Multiple Values</title>
		<link>http://www.otton.org/2009/11/22/more-php-switch-abuse-switching-on-multiple-values/</link>
		<comments>http://www.otton.org/2009/11/22/more-php-switch-abuse-switching-on-multiple-values/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 22:38:11 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[PHP Tricks]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[branching]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[switch]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=400</guid>
		<description><![CDATA[Due to working with Drupal for most of this year, I&#8217;ve seen this construct so many times it&#8217;s killed me a little inside:

switch&#40;$a&#41; &#123;
    case TRUE:
        switch&#40;$b&#41; &#123;
            case TRUE:
     [...]]]></description>
			<content:encoded><![CDATA[<p>Due to working with Drupal for most of this year, I&#8217;ve seen this construct so many times it&#8217;s killed me a little inside:</p>
<p><code>
<pre><span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">TRUE</span>:
        <span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$b</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">TRUE</span>:
                <span style="color: #b1b100;">break</span>;
            <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">FALSE</span>:
                <span style="color: #b1b100;">break</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">FALSE</span>:
        <span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$b</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">TRUE</span>:
                <span style="color: #b1b100;">break</span>;
            <span style="color: #b1b100;">case</span> <span style="color: #000000; font-weight: bold;">FALSE</span>:
                <span style="color: #b1b100;">break</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">break</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p></code></p>
<p>Someone I work with, who wishes to keep his name off the net, came up with this:</p>
<p><code>
<pre><span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span>, <span style="color: #0000ff;">$b</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">case</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">break</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p></code></p>
<p>I&#8217;d rather avoid the need for nested switches altogether, but this construct makes it easy to collapse similar branches, and is, I think, a useful addition to my toolkit:</p>
<p><code>
<pre><span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span>,<span style="color: #0000ff;">$b</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">case</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>:
        <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>:
    <span style="color: #b1b100;">case</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>:
    <span style="color: #b1b100;">case</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>:
    <span style="color: #000000; font-weight: bold;">default</span>:
        <span style="color: #b1b100;">break</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p></code></p>
<p>It&#8217;s worth mentioning that arrays are compared for equality (==) rather than identicality (===).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2009/11/22/more-php-switch-abuse-switching-on-multiple-values/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Supid PHP Tricks: Broken Switch</title>
		<link>http://www.otton.org/2009/06/05/supid-php-tricks-broken-switch/</link>
		<comments>http://www.otton.org/2009/06/05/supid-php-tricks-broken-switch/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 12:03:46 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[PHP Tricks]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=388</guid>
		<description><![CDATA[A fragment of PHP that really shouldn&#8217;t work. I hope nobody ever finds a use for this:

switch&#40;$n&#41;
&#123;
    case 1;
        echo 'one';
        break;
    case 2;
        echo 'two';
   [...]]]></description>
			<content:encoded><![CDATA[<p>A fragment of PHP that really shouldn&#8217;t work. I hope nobody ever finds a use for this:</p>
<p><code>
<pre><span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$n</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span>;
        <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'one'</span>;
        <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span>;
        <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'two'</span>;
        <span style="color: #b1b100;">break</span>;
    <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">3</span>;
        <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'three'</span>;
        <span style="color: #b1b100;">break</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2009/06/05/supid-php-tricks-broken-switch/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>I Learn Something New Every Day &#8211; isset()</title>
		<link>http://www.otton.org/2009/04/16/i-learn-something-new-every-day-isset/</link>
		<comments>http://www.otton.org/2009/04/16/i-learn-something-new-every-day-isset/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 12:10:30 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[PHP Tricks]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=377</guid>
		<description><![CDATA[Check this out:

if &#40;isset&#40;$a, $b, $c&#41;&#41;
&#123;
    unset&#40;$a, $b, $c&#41;;
&#125;

Mulitple arguments. Looks like they&#8217;ve been there since an early PHP4 release, at least. Why does nobody tell me these things?
]]></description>
			<content:encoded><![CDATA[<p>Check this out:<br />
<code>
<pre><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span>, <span style="color: #0000ff;">$b</span>, <span style="color: #0000ff;">$c</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <a href="http://www.php.net/unset"><span style="color: #000066;">unset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span>, <span style="color: #0000ff;">$b</span>, <span style="color: #0000ff;">$c</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p></code></p>
<p>Mulitple arguments. Looks like they&#8217;ve been there since an early PHP4 release, at least. Why does nobody tell me these things?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2009/04/16/i-learn-something-new-every-day-isset/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Script to make relative includes absolute, for the benefit of APC</title>
		<link>http://www.otton.org/2009/04/06/script-to-make-relative-includes-absolute-for-the-benefit-of-apc/</link>
		<comments>http://www.otton.org/2009/04/06/script-to-make-relative-includes-absolute-for-the-benefit-of-apc/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 17:16:29 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[build process]]></category>
		<category><![CDATA[include_once]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[require_once]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=371</guid>
		<description><![CDATA[The problem:
If you include() or require() files with relative paths, APC will search the include_path on every request, and you won&#8217;t gain the full benefit of turning apc.stat off.
Using absolute paths portably in your own code is easy &#8211; just use a constant, eg. &#8220;require APP_PATH . &#8216;file.php&#8217;;&#8221;. External libraries, however, are harder to deal [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The problem:</strong></p>
<p>If you include() or require() files with relative paths, <a href="http://pecl.php.net/package/APC">APC</a> will search the include_path on every request, and you won&#8217;t gain the full benefit of turning apc.stat off.</p>
<p>Using absolute paths portably in your own code is easy &#8211; just use a constant, eg. &#8220;require APP_PATH . &#8216;file.php&#8217;;&#8221;. External libraries, however, are harder to deal with &#8211; you either accept the performance hit, or go through them adding constants by hand (and merging your changes every time there&#8217;s a new release).</p>
<p><strong>The solution:</strong></p>
<p><a href="http://www.otton.org/uploads/Absolute/absolute.rar">This script</a> rewrites PHP scripts, turning relative paths into absolute paths, allowing you to deploy third party libraries within your build process and still take full advantage of APC.</p>
<p><strong>Some caveats:</strong></p>
<p>It reads from stdin and writes to stdout.</p>
<p>It relies on it&#8217;s own include_path; in the real world, you&#8217;ll probably want to pass that in on the command line.</p>
<p>It only deals with the most common subset of include statements (variations on T_INCLUDE, T_CONSTANT_ENCAPSED_STRING, T_SEMICOLON).</p>
<p>The Windows support is a bit wing-and-a-prayer.</p>
<p>It&#8217;s memory-greedy when dealing with very large files.</p>
<p>It doesn&#8217;t play 100% nicely with safe_mode_include_dir.</p>
<p>Thrown out there in the hope it will be useful to someone. I&#8217;d be delighted to accept any feedback or additions anyone wants to offer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2009/04/06/script-to-make-relative-includes-absolute-for-the-benefit-of-apc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Processing Blobs</title>
		<link>http://www.otton.org/2008/12/14/processing-blobs/</link>
		<comments>http://www.otton.org/2008/12/14/processing-blobs/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 21:02:03 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[processing]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=353</guid>
		<description><![CDATA[As Processing recently hit 1.0 I thought I&#8217;d take a quick look. It seems to work on the same principle as a unit test suite or a web framework &#8211; you develop &#8220;inside&#8221; the library, so it can abstract all that Java complexity away from you. Fill out setup() and draw() methods, press a button [...]]]></description>
			<content:encoded><![CDATA[<p>As Processing recently hit 1.0 I thought I&#8217;d take a quick look. It seems to work on the same principle as a unit test suite or a web framework &#8211; you develop &#8220;inside&#8221; the library, so it can abstract all that Java complexity away from you. Fill out setup() and draw() methods, press a button to run, press a button to export as an applet. Embedding the applet in Wordpress is actually a lot more frustrating than writing the damn thing (Solution: I resorted to an iframe. I don&#8217;t really want to waste time because Wordpress is being &#8220;smart&#8221;).</p>
<p>Processing is an awful lot of fun. It evokes a feeling of exploration that I haven&#8217;t had since the BBC Basic days &#8211; experimenting with maths, mapping it to a plane, just to see what it looks like.</p>
<p>I doubt anyone reading this via RSS will be able to see the applet, but you&#8217;re only missing lens-flare-esque coloured blobs. Click and hold to draw. Right-click to clear.</p>
<p><iframe src="http://www.otton.org/uploads/TransE/index.html" width="640" height="485" frameborder="0"></iframe></p>
<p>Source code: <a href="http://www.otton.org/uploads/TransE/TransparentEllipses.pde">TransparentEllipses</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2008/12/14/processing-blobs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Update: Hampshire Web Development</title>
		<link>http://www.otton.org/2008/11/27/update-hampshire-web-development/</link>
		<comments>http://www.otton.org/2008/11/27/update-hampshire-web-development/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 23:57:25 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[links]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=343</guid>
		<description><![CDATA[I&#8217;ve added Chris Dean to the list of web developers in Hampshire. I also updated the OPML summary.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve added <a href="http://www.coderchris.com/">Chris Dean</a> to the <a href="http://www.otton.org/2008/11/11/web-developers-portsmouth-southampton-hampshire/">list of web developers in Hampshire</a>. I also updated the <a href="http://www.otton.org/HampshireWebDevelopers.opml">OPML summary</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2008/11/27/update-hampshire-web-development/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>php://memory, Unit Tests</title>
		<link>http://www.otton.org/2008/11/18/unit-tests-for-myfputcsv-phpmemory/</link>
		<comments>http://www.otton.org/2008/11/18/unit-tests-for-myfputcsv-phpmemory/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 00:54:45 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[streams]]></category>
		<category><![CDATA[unit tests]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=330</guid>
		<description><![CDATA[I was trying to figure out the best way to test the myfputcsv() function I posted yesterday. Reading the data from disc before comparing it seemed like a step where errors could creep in, but there was no obvious way round that, as the purpose of the function is to write to disc.
Then I realised [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying to figure out the best way to test the myfputcsv() function I <a href="http://www.otton.org/2008/11/17/fputcsv-implementation-in-php/">posted yesterday</a>. Reading the data from disc before comparing it seemed like a step where errors could creep in, but there was no obvious way round that, as the purpose of the function is to write to disc.</p>
<p>Then I realised I could use PHP&#8217;s (fairly) new IO streams to dump the function&#8217;s output to a temporary buffer, and read it back in for comparison. Not perfect, but it removes concerns about file mutexes, permissions, unique filenames, etc. and speeds up the tests, as they never touch disc.</p>
<p>It wasn&#8217;t worth building on top of PHPUnit for this&#8230; checking failure conditions can wait for another day. I just wrote a quick script that compares the output of fputcsv() and myfputcsv(). (And it worked &#8211; I&#8217;ve already fixed two errors in yesterday&#8217;s post).</p>
<p>This is the first time I&#8217;ve reached for php://memory. It&#8217;s obviously useful for testing code that writes to disc, but I&#8217;m now wondering where else it might come in handy.</p>
<p><code>
<pre><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #ff0000;">'myfputcsv.php'</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> write<span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$function</span>, <span style="color: #0000ff;">$array</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$fp</span> = <a href="http://www.php.net/fopen"><span style="color: #000066;">fopen</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'php://memory'</span>, <span style="color: #ff0000;">'w+'</span> <span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$function</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fp</span>, <span style="color: #0000ff;">$array</span> <span style="color: #66cc66;">&#41;</span>;
    <a href="http://www.php.net/rewind"><span style="color: #000066;">rewind</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fp</span> <span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">return</span> <a href="http://www.php.net/fread"><span style="color: #000066;">fread</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fp</span>, <span style="color: #cc66cc;">1024</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> test<span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$array</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$fputcsv</span> = write<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'fputcsv'</span>, <span style="color: #0000ff;">$array</span> <span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$myfputcsv</span> = write<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'myfputcsv'</span>, <span style="color: #0000ff;">$array</span> <span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/assert"><span style="color: #000066;">assert</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'$fputcsv === $myfputcsv'</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">"OK"</span> . PHP_EOL;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
test<span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
test<span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">"Hello"</span>, <span style="color: #ff0000;">"World"</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
test<span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">"He<span style="color: #000099; font-weight: bold;">\n</span>llo"</span>, <span style="color: #ff0000;">"World"</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
test<span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">"He<span style="color: #000099; font-weight: bold;">\</span><span style="color: #000099; font-weight: bold;">\"</span>llo"</span>, <span style="color: #ff0000;">"World"</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
test<span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">"He llo"</span>, <span style="color: #ff0000;">"World"</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
test<span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">"He<span style="color: #000099; font-weight: bold;">\t</span>llo"</span>, <span style="color: #ff0000;">"World"</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
test<span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">"He<span style="color: #000099; font-weight: bold;">\"</span><span style="color: #000099; font-weight: bold;">\"</span>llo"</span>, <span style="color: #ff0000;">"World"</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
test<span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">"He,llo"</span>, <span style="color: #ff0000;">"World"</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2008/11/18/unit-tests-for-myfputcsv-phpmemory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>fputcsv() implementation in PHP</title>
		<link>http://www.otton.org/2008/11/17/fputcsv-implementation-in-php/</link>
		<comments>http://www.otton.org/2008/11/17/fputcsv-implementation-in-php/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 01:43:24 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=327</guid>
		<description><![CDATA[I recently needed an fputcsv() with a couple of modifications (I needed fields quoted unconditionally, and more than one character in the delimiter field). I looked at a couple of versions from the man page comments, but they were buggy in one way or another, and PHP4-specific.
The function below is as close as I can [...]]]></description>
			<content:encoded><![CDATA[<p>I recently needed an fputcsv() with a couple of modifications (I needed fields quoted unconditionally, and more than one character in the delimiter field). I looked at a couple of versions from the <a href="http://uk2.php.net/fputcsv">man page comments</a>, but they were buggy in one way or another, and PHP4-specific.</p>
<p>The function below is as close as I can get to fputcsv()&#8217;s behaviour. I&#8217;m throwing it out there in the hope that it will be useful to someone, someday. It should be easy enough to modify to suit specific requirements.</p>
<p><code>
<pre><span style="color: #808080; font-style: italic;">/**
 * myfputcsv()
 *
 * Mimics the observed behaviour of PHP's fputcsv()
 *
 * Requires at least PHP 5.2.0 due to reliance on __toString
 *
 * @param resource $fp valid file pointer
 * @param array $fields array of values
 * @param string $delimiter optional parameter sets the field delimiter character. Defaults to ','
 * @param string $enclosure optional parameter sets the field enclosure character. Defaults to '&quot;'
 * @return bool the length of the written string, or FALSE on failure
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> myfputcsv<span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fp</span>, <span style="color: #0000ff;">$fields</span>, <span style="color: #0000ff;">$delimiter</span> = <span style="color: #ff0000;">','</span>, <span style="color: #0000ff;">$enclosure</span> = <span style="color: #ff0000;">'&quot;'</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/**
     * Validate incoming values
     *
     * Weird corner cases are checked for here, so we can mimic fputcsv() as closely
     * as possible. Eg we check whether or not an object passed as $delimiter or
     * $enclosure implements __toString
     */</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> !<a href="http://www.php.net/is_resource"><span style="color: #000066;">is_resource</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fp</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <a href="http://www.php.net/trigger_error"><span style="color: #000066;">trigger_error</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">__FUNCTION__</span> . <span style="color: #ff0000;">'() expects parameter 1 to be resource, '</span> . <a href="http://www.php.net/gettype"><span style="color: #000066;">gettype</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fp</span> <span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">' given'</span>, <span style="color: #000000; font-weight: bold;">E_USER_WARNING</span> <span style="color: #66cc66;">&#41;</span>;
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> !<a href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fields</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <a href="http://www.php.net/trigger_error"><span style="color: #000066;">trigger_error</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">__FUNCTION__</span> . <span style="color: #ff0000;">'() expects parameter 2 to be array, '</span> . <a href="http://www.php.net/gettype"><span style="color: #000066;">gettype</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fields</span> <span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">' given'</span>, <span style="color: #000000; font-weight: bold;">E_USER_WARNING</span> <span style="color: #66cc66;">&#41;</span>;
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/is_object"><span style="color: #000066;">is_object</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$delimiter</span> <span style="color: #66cc66;">&#41;</span> &amp;&amp; <a href="http://www.php.net/method_exists"><span style="color: #000066;">method_exists</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$delimiter</span>, <span style="color: #ff0000;">'__toString'</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$delimiter</span> = <span style="color: #66cc66;">&#40;</span> string <span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">$delimiter</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/is_object"><span style="color: #000066;">is_object</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$enclosure</span> <span style="color: #66cc66;">&#41;</span> &amp;&amp; <a href="http://www.php.net/method_exists"><span style="color: #000066;">method_exists</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$enclosure</span>, <span style="color: #ff0000;">'__toString'</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$enclosure</span> = <span style="color: #66cc66;">&#40;</span> string <span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">$enclosure</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$delimiter</span> == <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <a href="http://www.php.net/trigger_error"><span style="color: #000066;">trigger_error</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">__FUNCTION__</span> . <span style="color: #ff0000;">'(): delimiter must be a character'</span>, <span style="color: #000000; font-weight: bold;">E_USER_WARNING</span> <span style="color: #66cc66;">&#41;</span>;
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$enclosure</span> == <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <a href="http://www.php.net/trigger_error"><span style="color: #000066;">trigger_error</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">__FUNCTION__</span> . <span style="color: #ff0000;">'(): enclosure must be a character'</span>, <span style="color: #000000; font-weight: bold;">E_USER_WARNING</span> <span style="color: #66cc66;">&#41;</span>;
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> !<a href="http://www.php.net/is_scalar"><span style="color: #000066;">is_scalar</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$delimiter</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <a href="http://www.php.net/trigger_error"><span style="color: #000066;">trigger_error</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">__FUNCTION__</span> . <span style="color: #ff0000;">'() expects parameter 3 to be string, '</span> . <a href="http://www.php.net/gettype"><span style="color: #000066;">gettype</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$delimiter</span> <span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">' given'</span>, <span style="color: #000000; font-weight: bold;">E_USER_WARNING</span> <span style="color: #66cc66;">&#41;</span>;
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> !<a href="http://www.php.net/is_scalar"><span style="color: #000066;">is_scalar</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$enclosure</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <a href="http://www.php.net/trigger_error"><span style="color: #000066;">trigger_error</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">__FUNCTION__</span> . <span style="color: #ff0000;">'() expects parameter 4 to be string, '</span> . <a href="http://www.php.net/gettype"><span style="color: #000066;">gettype</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$enclosure</span> <span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">' given'</span>, <span style="color: #000000; font-weight: bold;">E_USER_WARNING</span> <span style="color: #66cc66;">&#41;</span>;
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$delimiter</span> <span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <a href="http://www.php.net/trigger_error"><span style="color: #000066;">trigger_error</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">__FUNCTION__</span> . <span style="color: #ff0000;">'(): delimiter must be a single character'</span>, <span style="color: #000000; font-weight: bold;">E_USER_NOTICE</span> <span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$delimiter</span> = <span style="color: #0000ff;">$delimiter</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$enclosure</span> <span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <a href="http://www.php.net/trigger_error"><span style="color: #000066;">trigger_error</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">__FUNCTION__</span> . <span style="color: #ff0000;">'(): enclosure must be a single character'</span>, <span style="color: #000000; font-weight: bold;">E_USER_NOTICE</span> <span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$enclosure</span> = <span style="color: #0000ff;">$enclosure</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * Prepare fields for writing to file by escaping them and wrapping them
     * in $enclosure
     */</span>
    <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$i</span> = <span style="color: #cc66cc;">0</span>; <span style="color: #0000ff;">$i</span> &lt; <a href="http://www.php.net/sizeof"><span style="color: #000066;">sizeof</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fields</span> <span style="color: #66cc66;">&#41;</span>; <span style="color: #0000ff;">$i</span>++ <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">/**
         * Make a decision on whether or not to use $enclosure
         */</span>
        <span style="color: #0000ff;">$use_enclosure</span> = <span style="color: #000000; font-weight: bold;">false</span>;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/strpos"><span style="color: #000066;">strpos</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #0000ff;">$delimiter</span> <span style="color: #66cc66;">&#41;</span> !== <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$use_enclosure</span> = <span style="color: #000000; font-weight: bold;">true</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/strpos"><span style="color: #000066;">strpos</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #0000ff;">$enclosure</span> <span style="color: #66cc66;">&#41;</span> !== <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$use_enclosure</span> = <span style="color: #000000; font-weight: bold;">true</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/strpos"><span style="color: #000066;">strpos</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\</span>"</span> <span style="color: #66cc66;">&#41;</span> !== <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$use_enclosure</span> = <span style="color: #000000; font-weight: bold;">true</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/strpos"><span style="color: #000066;">strpos</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span> <span style="color: #66cc66;">&#41;</span> !== <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$use_enclosure</span> = <span style="color: #000000; font-weight: bold;">true</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/strpos"><span style="color: #000066;">strpos</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\r</span>"</span> <span style="color: #66cc66;">&#41;</span> !== <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$use_enclosure</span> = <span style="color: #000000; font-weight: bold;">true</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/strpos"><span style="color: #000066;">strpos</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\t</span>"</span> <span style="color: #66cc66;">&#41;</span> !== <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$use_enclosure</span> = <span style="color: #000000; font-weight: bold;">true</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/strpos"><span style="color: #000066;">strpos</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">" "</span> <span style="color: #66cc66;">&#41;</span> !== <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$use_enclosure</span> = <span style="color: #000000; font-weight: bold;">true</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$use_enclosure</span> == <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span> = <a href="http://www.php.net/explode"><span style="color: #000066;">explode</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\</span>$enclosure"</span>, <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;
            <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$j</span> = <span style="color: #cc66cc;">0</span>; <span style="color: #0000ff;">$j</span> &lt; <a href="http://www.php.net/sizeof"><span style="color: #000066;">sizeof</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>; <span style="color: #0000ff;">$j</span>++ <span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$j</span><span style="color: #66cc66;">&#93;</span> = <a href="http://www.php.net/explode"><span style="color: #000066;">explode</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$enclosure</span>, <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$j</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;
                <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$j</span><span style="color: #66cc66;">&#93;</span> = <a href="http://www.php.net/implode"><span style="color: #000066;">implode</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">"{$enclosure}{$enclosure}"</span>, <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$j</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span> = <a href="http://www.php.net/implode"><span style="color: #000066;">implode</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\</span>$enclosure"</span>, <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;
            <span style="color: #0000ff;">$fields</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">"{$enclosure}{$fields[$i]}{$enclosure}"</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
     * Write fields as a $delimiter-delimited string, and return number of
     * bytes written
     */</span>
    <span style="color: #b1b100;">return</span> <a href="http://www.php.net/fwrite"><span style="color: #000066;">fwrite</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$fp</span>, <a href="http://www.php.net/implode"><span style="color: #000066;">implode</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$delimiter</span>, <span style="color: #0000ff;">$fields</span> <span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2008/11/17/fputcsv-implementation-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP Tip: Classes Aren&#8217;t Derived From stdClass</title>
		<link>http://www.otton.org/2008/11/11/php-base-class-stdclass/</link>
		<comments>http://www.otton.org/2008/11/11/php-base-class-stdclass/#comments</comments>
		<pubDate>Tue, 11 Nov 2008 17:49:54 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[PHP Tricks]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=324</guid>
		<description><![CDATA[A base class is &#34;a class from which other classes are derived&#34;.
Many OO languages have the concept of a single base class from which all other classes are explicitly or implicitly descended. For example, Ruby, Java and .NET all have Object.
It&#8217;s a very common belief that PHP implements stdClass as a base class for all [...]]]></description>
			<content:encoded><![CDATA[<p>A base class is &quot;<a href="http://en.wikipedia.org/wiki/Superclass_(computer_science)">a class from which other classes are derived</a>&quot;.</p>
<p>Many OO languages have the concept of a single base class from which all other classes are explicitly or implicitly descended. For example, Ruby, Java and .NET all have <tt>Object</tt>.</p>
<p>It&#8217;s a very <a href="http://www.google.co.uk/search?hl=en&#038;q=php%20%22base%20class%22%20stdClass&#038;meta=">common belief</a> that PHP implements stdClass as a base class for all objects, but this is in fact not the case:</p>
<p><code>
<pre><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> DoesNotExtend <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> DoesExtend extends stdClass <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$doesNotExtend</span> = <span style="color: #000000; font-weight: bold;">new</span> DoesNotExtend<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$doesExtend</span> = <span style="color: #000000; font-weight: bold;">new</span> DoesExtend<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<a href="http://www.php.net/var_dump"><span style="color: #000066;">var_dump</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$doesNotExtend</span> instanceof stdClass<span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/var_dump"><span style="color: #000066;">var_dump</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$doesExtend</span> instanceof stdClass<span style="color: #66cc66;">&#41;</span>;</pre>
<p></code></p>
<p>Outputs:</p>
<p>bool(false)<br />
bool(true)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2008/11/11/php-base-class-stdclass/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Web Development in Hampshire</title>
		<link>http://www.otton.org/2008/11/11/web-developers-portsmouth-southampton-hampshire/</link>
		<comments>http://www.otton.org/2008/11/11/web-developers-portsmouth-southampton-hampshire/#comments</comments>
		<pubDate>Tue, 11 Nov 2008 10:32:46 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[personal]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[hampshire]]></category>
		<category><![CDATA[portsmouth]]></category>
		<category><![CDATA[southampton]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.otton.org/?p=284</guid>
		<description><![CDATA[When I started in web development a few years ago I had to move to Birmingham and London to find work &#8211; the local web industry was pretty much DOA. I&#8217;m not sure why that was&#8230; possibly companies gravitated towards the fibre in Docklands and the financing in the city, or London&#8217;s pool of experienced [...]]]></description>
			<content:encoded><![CDATA[<p>When I started in web development a few years ago I had to move to Birmingham and London to find work &#8211; the local web industry was pretty much DOA. I&#8217;m not sure why that was&#8230; possibly companies gravitated towards the fibre in Docklands and the financing in the city, or London&#8217;s pool of experienced workers was larger. Anyway, technology on the South Coast tended to be focused on defence contracting.</p>
<p>Today there are certainly more developers out there, but they&#8217;re not very visible &#8211; a Google search for &#8220;PHP Developer Portsmouth&#8221; throws up pages of job ads, rather than pages of local PHP developers. Other technology- and location-specific searches return similar results.</p>
<p>In an effort to change this, and to make the local development community a bit more &#8220;joined-up&#8221;, I&#8217;ve been collecting local developers&#8217; blogs. I&#8217;m hopeful that in the long-term these blogs will form the core of a local web development community, but at the very least, if we talk to each other, if we&#8217;re active online and network around subjects of common interest, it will raise everyone&#8217;s profile. I know that Adam Wintle wants to do similar things over at <a href="http://www.refreshportsmouth.org/">Refresh Portsmouth</a>, so anyone who&#8217;s keen on real-world meet-ups should <a href="http://www.mallmusmedia.com/contact">talk to him</a>.</p>
<p>If you&#8217;re at all interested in web development on the South Coast, please import this <a href="http://www.otton.org/HampshireWebDevelopers.opml">handy OPML file</a> into your RSS reader, and check back for updates occasionally. Alternatively, the HTML list is after the break. If you want to be added to the list, or you know someone who should be on there, please just <a href="http://www.otton.org/contact-me/">let me know</a>.<br />
<span id="more-284"></span><br />
<strong><a href="http://www.otton.org/HampshireWebDevelopers.opml">OPML Summary</a></strong></p>
<p><strong><a href="http://www.dazecoop.co.uk/">David Cooper</a></strong></p>
<p>David is a 21-year-old website developer and designer from Hampshire, UK. His blog &#8211; dazecoop &#8211; includes a portfolio of the best-of images from David along with interests including Minis, Cars,<br />
Motorsport, Photography, PHP Development, Website Design, Google and lots more.</p>
<p><strong><a href="http://www.coderchris.com/">Chris Dean</a></strong></p>
<p>Chris is both Projects Manager and a PHP web developer for Netbasic Ltd. in Fareham.<br />
His specialities include OOP PHP 5, MySQL DBA, streaming media and wordpress monetization, and due to having a scientific background tends to bring those skills into his development work too. He contributes regularly to the linkedin questions forum on blogging and web development, and runs a number of blogs on a wide range of subjects ranging from household pets to niche scientific topics.<br />
Though a full time employee, he is always on the look out for technically interesting and engaging short term contracts. He <a href="http://www.coderchris.com/">posts</a> about LAMP development and technology in general.</p>
<p><strong><a href="http://blog.durdle.com/">Howard Durdle</a></strong></p>
<p>Howard is a geek, a gamer, a company director and sometime surfer. He watches a lot of American TV. He lives in Winchester, and currently contracts for the largest software company in the world. He&#8217;s been involved in all kinds of interesting stuff, from ASP/PHP through to iPhone application development, electronics design and network security for the MOD.</p>
<p><strong><a href="http://www.andyjarrett.co.uk/andy/blog/index.cfm">Andy Jarrett</a></strong></p>
<p>Andy is a Senior ColdFusion developer, located in Portsmouth, UK</p>
<p><strong><a href="http://macdan.net/">Daniel Kendell</a></strong></p>
<p>Daniel is primarily a PHP developer in Fareham. He loves LEDs, most aspects of technology, philosophy and often gets confused in the middle of his own sentences. Not thick, just a bit dense.</p>
<p><strong><a href="http://www.yetanotherblog.com/">Sven Latham</a></strong></p>
<p>Sven is a freelance IT and systems consultant, based in Gosport. He&#8217;s planning a series of articles based on his experiences setting up his own IT company.</p>
<p><strong><a href="http://www.otton.org/">David Otton</a></strong></p>
<p>David is a web developer, currently working in Portsmouth. He has a love/hate relationship with PHP, and thinks the world could use more giant rabbits.</p>
<p><strong><a href="http://blog.mauveweb.co.uk/">Daniel Pope</a></strong></p>
<p>Daniel is a Django developer in Chichester (which is close enough). His blog is pretty active, and he writes posts in some interesting areas, such as Python and SVG.</p>
<p><strong><a href="http://www.velcro-city.co.uk/">Paul Graham Raven</a></strong></p>
<p>By day Paul is a rather shabby library assistant in a local museum, but in the evenings he magically transforms into a freelance writer, publicist, web presence manager and social media consultant, webzine editor, book and music reviewer, poet, guitarist and aspiring science fiction short story author. He manages to fit some sleep in every now and again, too.</p>
<p><strong><a href="http://rich.saekang.co.uk/">Rich Sae Kang</a></strong></p>
<p>Rich is a Portsmouth-based web developer and IT manager. His posts currently lean towards shell scripting and music.</p>
<p><strong><a href="http://echohelloworld.com/">Jasper Tandy</a></strong></p>
<p>Jasper is a salaried/freelance web developer in Southampton. He lives with a rabbit and a kitten, which is cool.</p>
<p><strong><a href="http://www.asgrim.com/">James Titcumb</a></strong></p>
<p>James is a PHP web developer living in Whiteley. He works in Fareham for Netbasic. He&#8217;s also a Twitter addict, making his blog one of the most frequently updated on this list.</p>
<p><strong><a href="http://www.get2dom.com/">Dominic Winsor</a></strong></p>
<p>Dominic is a user-experience consultant in Southampton. He&#8217;s an ethical shopper, and web developer, usability expert and interaction designer with experience of working with everything from small businesses and charities through to high-street banks.</p>
<p><strong><a href="http://www.mallmusmedia.com/the-blog">Adam Wintle</a></strong></p>
<p>Adam is a Portsmouth-based developer and designer. He appears to have almost scary levels of motivation.</p>
<p><strong><a href="http://priscimon.com/blog/">Eddy Young</a></strong></p>
<p>Eddy is a Hampshire-based Java/C# programmer. His blog tends towards photography.</p>
<p><strong><a href="http://www.otton.org/HampshireWebDevelopers.opml">OPML Summary</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.otton.org/2008/11/11/web-developers-portsmouth-southampton-hampshire/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
