<?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>Ash Searle's Blog &#187; PHP</title>
	<atom:link href="http://hexmen.com/blog/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://hexmen.com/blog</link>
	<description>On programming, and other things...</description>
	<lastBuildDate>Thu, 03 Dec 2009 10:29:14 +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>Concatenating arrays in&#160;PHP</title>
		<link>http://hexmen.com/blog/2008/11/concatenating-arrays-in-php/</link>
		<comments>http://hexmen.com/blog/2008/11/concatenating-arrays-in-php/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 11:46:48 +0000</pubDate>
		<dc:creator>Ash</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tip]]></category>

		<guid isPermaLink="false">http://hexmen.com/blog/?p=69</guid>
		<description><![CDATA[Just a quick post so I know where to look the next time I forget how to concatenate arrays in PHP. Use array_merge to concatenate two numerically-indexed arrays; not array_push and not the array union operator: +. $first = array('doh', 'ray', 'me'); $second = array('fah', 'soh', 'lah', 'te', 'do'); echo "Union: ", var_export($first + $second, [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick post so I know where to look the next time I forget how to concatenate arrays in PHP.</p>
<p>Use <a href="http://php.net/array_merge"><code>array_merge</code></a> to concatenate two numerically-indexed arrays; not <a href="http://php.net/array_push"><code>array_push</code></a> and not <a href="http://php.net/language.operators.array">the array union operator: <code>+</code></a>.</p>
<pre><code class="php">
$first = array('doh', 'ray', 'me');
$second = array('fah', 'soh', 'lah', 'te', 'do');

echo "Union: ", var_export($first + $second, true), "n";
echo "Merge: ", var_export(array_merge($first, $second), true), "n";

// array_push returns int, not an array:
array_push($first, $second);
echo "Push: ", var_export($first, true), "n";
</code></pre>
<p>The output:</p>
<pre>
Union: array (
  0 => 'doh',
  1 => 'ray',
  2 => 'me',
  3 => 'te',
  4 => 'do',
)
Merge: array (
  0 => 'doh',
  1 => 'ray',
  2 => 'me',
  3 => 'fah',
  4 => 'soh',
  5 => 'lah',
  6 => 'te',
  7 => 'do',
)
Push: array (
  0 => 'doh',
  1 => 'ray',
  2 => 'me',
  3 =>
  array (
    0 => 'fah',
    1 => 'soh',
    2 => 'lah',
    3 => 'te',
    4 => 'do',
  ),
)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://hexmen.com/blog/2008/11/concatenating-arrays-in-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP Session Management (grievance&#160;2)</title>
		<link>http://hexmen.com/blog/2008/08/php-session-management-grievance-2/</link>
		<comments>http://hexmen.com/blog/2008/08/php-session-management-grievance-2/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 10:15:20 +0000</pubDate>
		<dc:creator>Ash</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://hexmen.com/blog/?p=56</guid>
		<description><![CDATA[Sometimes PHP surprises you with an easy-to-use feature, like sessions. Sessions are quite easy to use in PHP. One call to @session_start(), and you have a magic global called $_SESSION to store data in; associated with the user using a cookie called PHPSESSID. PHP takes care of reading and writing the session data for you, [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes PHP surprises you with an easy-to-use feature, like sessions.</p>
<p>Sessions are quite easy to use in PHP.  One call to <code>@session_start()</code>, and you have a magic global called <code>$_SESSION</code> to store data in; associated with the user using a cookie called <code>PHPSESSID</code>.  PHP takes care of reading and writing the session data for you, and you think no more about it.</p>
<p>Simple.</p>
<p>Time passes, and you haven&#8217;t given sessions another thought.  Your site&#8217;s evolving, using more and more AJAX, and seems to be performing &#8216;OK&#8217;.  But, there&#8217;s a niggling doubt that something&#8217;s not quite right.</p>
<p>For us, we realized something was wrong when we opened multiple search-results in separate windows.  We could see the tabs were loading one by one, slowly.</p>
<p>I guess we should have paid more attention to start with.  Our previous web development background revolved around enterprise-class application servers.  Sessions just worked, no concurrency worries.  If you happened to run into a race-condition, you worked around it using threading and locking facilities provided by the implementation language.  It never occurred to us that PHP would be so different.</p>
<p>PHP, the way we&#8217;re running it (via mod_php) couldn&#8217;t be further from the application-server model if it tried.  (By default) sessions are implemented using file-based storage, not held in shared memory ready for use by multiple threads.</p>
<p>Storing sessions in files means PHP has to take heavy-handed precautions against concurrent read/write access to the session &#8211; it locks the session file for the duration of a request.</p>
<p>The idea never occurred to us &#8211; that session management would block user-requests, stopping concurrent requests completing (think AJAX.)  Fortunately the quick-fix solution is simple:  call <a href="http://php.net/session_write_close">session_write_close()</a> as soon as you&#8217;ve finished writing to the session.  Depending how you use sessions, you may find a number of actions only need read-access to the session, in which case you may want to open and close the session together: <code>@session_start(); session_write_close()</code></p>
<p>That&#8217;s the quick fix, but there are plenty of other options to explore to.  A quick code-audit could identify a ton of actions, controllers and pages that simply don&#8217;t need session access at all.  Now you know PHP locks the session file, you probably want to avoid calling <code>session_start()</code> unless absolutely necessary.</p>
<p>Secondly, PHP allows you to choose what type of session-management you use.  You can use  <a href="http://www.eu.socialtext.net/memcached/index.cgi?sessions">memcached</a> either on its own, or with a database backing-store.  You could use a MySQL back-end, or roll your own session management registered using <a href="http://uk2.php.net/manual/en/function.session-set-save-handler.php">session_set_save_handler</a>.  It&#8217;s really up to you.</p>
<p>Perhaps that&#8217;s the problem right there.  All the session-management hooks are there because the default session management sucks.  The simplicity of using sessions lulls you into a false sense of security, but make no mistake &#8211; sessions need to be handled with care if you&#8217;ve any hope of running a high-volume website.</p>
<p>Are your sessions managed properly?</p>
]]></content:encoded>
			<wfw:commentRss>http://hexmen.com/blog/2008/08/php-session-management-grievance-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP grievance number&#160;1</title>
		<link>http://hexmen.com/blog/2008/07/php-grievance-number-1/</link>
		<comments>http://hexmen.com/blog/2008/07/php-grievance-number-1/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 18:44:19 +0000</pubDate>
		<dc:creator>Ash</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Rant]]></category>

		<guid isPermaLink="false">http://hexmen.com/blog/?p=51</guid>
		<description><![CDATA[There&#8217;s a lot to hate about PHP. Maybe that&#8217;s harsh: nothing&#8217;s perfect, every language has it&#8217;s strength and weaknesses, and noone ever suggested using PHP for everything. Bearing that in mind, I&#8217;m using PHP daily, and you get used to a lot of quirks and foibles, and it&#8217;s easy to forget how truly shit it [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a lot to hate about PHP.</p>
<p>Maybe that&#8217;s harsh: nothing&#8217;s perfect, every language has it&#8217;s strength and weaknesses, and noone ever suggested using PHP for <em>everything</em>.</p>
<p>Bearing that in mind, I&#8217;m using PHP <em>daily</em>, and you get used to a lot of quirks and foibles, and it&#8217;s easy to forget how truly shit it is.</p>
<p>Take arrays for example.  Please; please take &#8216;em.</p>
<p>Skipping the &#8220;needle, haystack&#8221; parameter ordering farce, there are two things I dislike.</p>
<ol>
<li>array access warnings</li>
<li>array access</li>
</ol>
<p>First, the warnings.</p>
<p>The language designers must have made a choice between checked vs. unchecked array access:  should this throw a warning, or shouldn&#8217;t it:</p>
<pre><code>
$titles = array("Philosopher's Stone", "Chamber of Secrets");

// accessing an element that doesn't exist:
$title = $titles[2];
</code></pre>
<p>They decided it should &#8211; it&#8217;s just a &#8220;Notice&#8221;; easily hidden with appropriate <code>php.ini</code> settings.  I can live with that;  it&#8217;s been a while, but doesn&#8217;t Java throws a wobbly when you fall off the end of an array too?</p>
<p>My grievance is here, when you make a typo:</p>
<pre><code>
$titles = array("Philosopher's Stone", "Chamber of Secrets");
$tiltes[2] = "Prisoner of Azkaban";
</code></pre>
<p>No errors, no warnings, no notices.  Idiot-fingers just created a new array <code>$tiltes</code>, assigning a vitally important piece of information to a variable that shouldn&#8217;t exist!</p>
<p>That&#8217;s a bit stupid, isn&#8217;t it?  Bit of a design-decision inconsistency?</p>
<p>Onto array access: it would be nice &#8211; and when I say nice, I mean <em>obvious, natural and expected</em> &#8211; to write:</p>
<pre><code>
$head = $dom-&gt;get_elements_by_tagname('head')[0];
</code></pre>
<p>PHP&#8217;s <code>getElementsByTagName</code> returns an <code>array</code>; we&#8217;re expecting a single <code>&lt;head&gt;</code> element, so try to skip the bullshit and access it straight-off.  But we can&#8217;t, because PHP&#8217;s compiler can&#8217;t handle array-access on function return-values.</p>
<p>Object access?  Sure, no problem.  Calling a method of an object in an array inside another array is no problem:</p>
<p><code>$book['chapter']['title']-&gt;display('html')</code></p>
<p>But if you want to access an array element returned from a function, choose a different language.</p>
]]></content:encoded>
			<wfw:commentRss>http://hexmen.com/blog/2008/07/php-grievance-number-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Upgrading WordPress using&#160;mysqli</title>
		<link>http://hexmen.com/blog/2007/05/upgrade-wordpress/</link>
		<comments>http://hexmen.com/blog/2007/05/upgrade-wordpress/#comments</comments>
		<pubDate>Thu, 10 May 2007 07:25:29 +0000</pubDate>
		<dc:creator>Ash</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://hexmen.com/blog/2007/05/upgrade-wordpress/</guid>
		<description><![CDATA[Out-of-the-box WordPress assumes it&#8217;s going to be talking to a MySQL database using PHP&#8217;s original mysql extension instead of the newer mysqli extension (true up to WordPress 2.1.3 at least). If for some reason you need to run using mysqli, there are simple instructions on the WordPress support site: MySQLi for WordPress 2.1.x. Switching to [...]]]></description>
			<content:encoded><![CDATA[<p>Out-of-the-box WordPress assumes it&#8217;s going to be talking to a MySQL database using PHP&#8217;s original mysql extension instead of the newer mysql<em>i</em> extension (true up to WordPress 2.1.3 at least).  If for some reason you need to run using mysqli, there are simple instructions on the WordPress support site: <a href="http://wordpress.org/support/topic/110026">MySQLi for WordPress 2.1.x</a>.</p>
<p>Switching to MySQLi was easy and worked as described, except for one thing: I couldn&#8217;t upgrade WordPress.  After a bit of sleuthing I found there&#8217;s a MySQL version check buried in the upgrade process.  For the current version (WordPress 2.1.3) it&#8217;s checking that MySQL is version 4.0.0 or higher.  According to the <a href="http://uk3.php.net/mysqli">PHP documentation for mysqli</a>, it needs to run on MySQL 4.1 or above &#8211; so the version check is redundant.</p>
<p>Short-term fix: follow the first few steps of the <a href="http://codex.wordpress.org/Upgrading_WordPress">WordPress upgrade process</a> but before <a href="http://codex.wordpress.org/Upgrading_WordPress#Step_9:_Run_the_WordPress_upgrade_program">running the upgrade program</a> edit <code>wp-admin/upgrade-functions.php</code> and gut this function:</p>
<pre><code class="php">function wp_check_mysql_version() {
    // (version-check removed)
}</code></pre>
<p>You should be able to run the upgrade program fine now.</p>
]]></content:encoded>
			<wfw:commentRss>http://hexmen.com/blog/2007/05/upgrade-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A handy PHP debug&#160;function</title>
		<link>http://hexmen.com/blog/2007/05/php-debug-function/</link>
		<comments>http://hexmen.com/blog/2007/05/php-debug-function/#comments</comments>
		<pubDate>Thu, 03 May 2007 17:19:44 +0000</pubDate>
		<dc:creator>Ash</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://hexmen.com/blog/2007/05/php-debug-function/</guid>
		<description><![CDATA[Like most developers, I often find it useful to spit out a few messages in the middle of a PHP script &#8211; especially when I&#8217;m debugging some problems. Sadly, after only a few months our pristine code-base has become spotted with this, over and over again: if (DEBUG) { echo "New user object: "; var_dump($user); [...]]]></description>
			<content:encoded><![CDATA[<p>Like most developers, I often find it useful to spit out a few messages in the middle of a PHP script &#8211; especially when I&#8217;m debugging some problems.</p>
<p>Sadly, after only a few months our pristine code-base has become spotted with this, over and over again:</p>
<pre><code class="php">if (DEBUG) {
    echo "New user object: "; var_dump($user); echo "\\n";
}</code></pre>
<p>After discovering <code>var_export</code> I felt like I&#8217;d improved things by switching to:</p>
<pre><code class="php">if (DEBUG) {
    printf("New user object: %s\\n", var_export($user, true));
}</code></pre>
<p>I still didn&#8217;t like the fact that <code>if (DEBUG)</code> was appearing all over the place.  So, after stumbling across <a href="http://uk2.php.net/manual/en/function.call-user-func-array.php">call_user_func_array</a> I&#8217;ve implemented by own <code>debug</code> function:</p>
<pre><code class="php">function debug()
{
    if (DEBUG)
    {
	$args = func_get_args();
	$args[0] = "&lt;pre&gt;" . $args[0] . "&lt;/pre&gt;\\n";
	for ($i = 1, $l = count($args); $i &lt; $l; $i++) {
	    $args[$i] = htmlspecialchars(var_export($args[$i], true));
	}
	call_user_func_array('printf', $args);
    }
}</code></pre>
<p>Which means I can remove all the <code>if (DEBUG)</code> statements, leaving only:</p>
<pre><code class="php">debug("New user object: %s", $user);</code></pre>
<p>I think that&#8217;s much cleaner.</p>
]]></content:encoded>
			<wfw:commentRss>http://hexmen.com/blog/2007/05/php-debug-function/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
