<?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>Jamie Thompson &#187; PHP</title>
	<atom:link href="http://jamiethompson.co.uk/tags/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://jamiethompson.co.uk</link>
	<description>Web Developer</description>
	<lastBuildDate>Tue, 08 May 2012 10:58:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Yoda Syntax &#8211; A Backwards Design Pattern for If Statements</title>
		<link>http://jamiethompson.co.uk/web/2010/05/20/yoda-syntax-a-php-design-pattern-for-if-statements/</link>
		<comments>http://jamiethompson.co.uk/web/2010/05/20/yoda-syntax-a-php-design-pattern-for-if-statements/#comments</comments>
		<pubDate>Thu, 20 May 2010 15:00:58 +0000</pubDate>
		<dc:creator>Jamie Thompson</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jamiethompson.co.uk/?p=483</guid>
		<description><![CDATA[
Sometimes the penny drops and you realise there&#8217;s a childishly simple solution to a problem that&#8217;s been bugging you for years. And no, I&#8217;m not talking about my wife, I&#8217;m talking about Yoda Syntax.
Have you ever spent far too long hunting down an impossible bug in your PHP only to eventually track it down to [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://jamiethompson.co.uk/wp-content/uploads/2010/05/yoda_syntax.jpg" alt="Yoda Syntax" title="Yoda Syntax" width="530" height="190" class="alignnone size-full wp-image-486" /></p>
<p>Sometimes the penny drops and you realise there&#8217;s a childishly simple solution to a problem that&#8217;s been bugging you for years. And no, I&#8217;m not talking about my wife, I&#8217;m talking about Yoda Syntax.</p>
<p>Have you ever spent far too long hunting down an impossible bug in your PHP only to eventually track it down to a typo in an if statement. Chances are it was a misplaced &#8216;=&#8217;.</p>
<h2>Named must your fail be before banish it you can</h2>
<p>We all do it, and more often than we&#8217;d probably like to admit. Unless you don&#8217;t, in which case go pat yourself on the back, you&#8217;re a PHP rockstar. But as a simplified example say you wanted to write the following rather pointless function:</p>
<pre class="brush: php">
function is_it_red($colour) {
    if ($colour == 'red') {
         return true;
    }
}
</pre>
<p>but what you ended up writing was</p>
<pre class="brush: php">
function is_it_red($colour) {
    if ($colour = 'red') {
         return true;
    }
}
</pre>
<p>And as you know, whatever you pass into the second function (because you&#8217;re accidentally performing a variable assignment rather than testing for equality) it will return true.<br />
Obviously this is an oversimplified example, but typos like this nestled inside large classes can prove hair-removingly difficult to track down.</p>
<h2>Help you I can, yes</h2>
<p>The solution to this is simply to alter the way you write if statements:</p>
<pre class="brush: php">
function is_it_red($colour) {
    if ('red' == $colour) {
         return true;
    }
}
</pre>
<p>It looks odd, yeah, and you might have seen other people writing their if statements backwards in the past and wondered why. But the benefit of this is striking, and so simple that I&#8217;m left wondering why I never thought of it before.</p>
<p>Quite simply, if you make a typo and write the following:</p>
<pre class="brush: php">
function is_it_red($colour) {
    if ('red' = $colour) {
         return true;
    }
}
</pre>
<p>You will cause a big nasty syntax error that you simply cannot fail to notice.</p>
<p><strong>Parse error: syntax error, unexpected &#8216;=&#8217; in /var/www/vhosts/example.com/httpdocs/what_colour_is_it.php</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://jamiethompson.co.uk/web/2010/05/20/yoda-syntax-a-php-design-pattern-for-if-statements/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Expanding Short URLs with PHP: expand_url PHP function</title>
		<link>http://jamiethompson.co.uk/web/2010/05/18/expanding-short-urls-with-php-expand_url-php-function/</link>
		<comments>http://jamiethompson.co.uk/web/2010/05/18/expanding-short-urls-with-php-expand_url-php-function/#comments</comments>
		<pubDate>Tue, 18 May 2010 01:59:31 +0000</pubDate>
		<dc:creator>Jamie Thompson</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jamiethompson.co.uk/?p=430</guid>
		<description><![CDATA[
URL shortening services work by redirecting a users browser from the shortened URL to the actual target URL. Some of these services provide means of reversing the shortening process. Some provide API methods for this.
The following function will take a shortened URL, from any service, and return the original URL. As it works by inspecting [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://jamiethompson.co.uk/wp-content/uploads/2010/05/301.jpg" alt="PHP Short URL Expander Function" title="PHP Short URL Expander Function" width="530" height="190" class="alignnone size-full wp-image-433" /></p>
<p>URL shortening services work by redirecting a users browser from the shortened URL to the actual target URL. Some of these services provide means of reversing the shortening process. <a href="http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/expand">Some</a> provide API methods for this.</p>
<p>The following function will take a shortened URL, from <strong>any</strong> service, and return the original URL. As it works by inspecting the HTTP redirect headers it is 100% service independent, and does away with the need to query the API of services like bit.ly for url expansion.</p>
<pre class="brush: php">
function expand_url($url) {

	// Use curl to fetch the HTTP Headers
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_HEADER, 1); // just the header
	curl_setopt($ch, CURLOPT_NOBODY, 1); // not the body
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$output = curl_exec($ch);
	preg_match('/Location: (.*)\n/', $output, $matches);

	// if no redirect header then return the original url
	return isset($matches[1]) ? $matches[1] : $url;

}
</pre>
<h2>A Simpler PHP 5 Version</h2>
<p>I&#8217;ve used curl for portability. PHP 5 has a built in get_headers function which you could use to simplify the function a little. It would look something like this:</p>
<pre class="brush: php">
function expand_url($url) {

	$h = get_headers($url);

	// if no redirect header then return the original url
	return isset($h['Location']) ? $h['Location'] : $url;

}
</pre>
<div class="pullout">
<h2>Creating Short URLs With PHP</h2>
<p>For creating short urls on the fly with PHP see: <a href="http://tinyurl.com/php-shorten-url">PHP shorten_url function</a> </p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jamiethompson.co.uk/web/2010/05/18/expanding-short-urls-with-php-expand_url-php-function/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Create Short URLs with PHP: shorten_url PHP Function</title>
		<link>http://jamiethompson.co.uk/web/2010/05/18/create-short-urls-with-php-shorten_url-php-function/</link>
		<comments>http://jamiethompson.co.uk/web/2010/05/18/create-short-urls-with-php-shorten_url-php-function/#comments</comments>
		<pubDate>Tue, 18 May 2010 00:45:31 +0000</pubDate>
		<dc:creator>Jamie Thompson</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jamiethompson.co.uk/?p=416</guid>
		<description><![CDATA[
Do you want to create short urls from within your own PHP application? Most url shortening services provide an API, it&#8217;s how apps like twitter clients provide built-in url shortening. I wrote a small PHP function to provide url shortening from two of the most popular services, is.gd and tinyurl.com. Where&#8217;s bit.ly I hear you [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://jamiethompson.co.uk/wp-content/uploads/2010/05/tape_measure.jpg" alt="Create Short URLs with PHP" title="tape_measure" width="530" height="190" class="size-full wp-image-425" /></p>
<p>Do you want to create short urls from within your own PHP application? Most url shortening services provide an API, it&#8217;s how apps like twitter clients provide built-in url shortening. I wrote a small PHP function to provide url shortening from two of the most popular services, <a href="http://is.gd">is.gd</a> and <a href="http://tinyurl.com">tinyurl.com</a>. Where&#8217;s <a href="http://bit.ly">bit.ly</a> I hear you ask. Well yeah, sure, that&#8217;s what all the cool kids are using these days, but bit.ly <a href="http://code.google.com/p/bitly-api/wiki/ApiDocumentation#Authentication_and_Shared_Parameters">requires an API key</a>. That&#8217;s not a huge issue, it&#8217;s free. It just doesn&#8217;t sit well with creating a nice portable, drop in a go PHP function. So bit.ly didn&#8217;t get an invite to my super mega PHP function party.</p>
<p>If you want to get some bit.ly action on the go then feel free to modify the code below accordingly. I might update this at some point. I keep thinking I should, but meh.</p>
<pre class="brush: php">
function shorten_url($url,$service='tinyurl.com') {

	// create the request url based on the selected shortening service
	switch ($service) {
	    case 'tinyurl.com':
	       $service_url = 'http://tinyurl.com/api-create.php?url='.urlencode($url);
		   break;
	    case 'is.gd':
	        $service_url = 'http://is.gd/api.php?longurl='.urlencode($url);
	}

	/*
	 * use cURL to fetch the respons
	 * Feel free to swap this out for
	 * $output = file_get_contents($service_url)
	 * if you have fopen wrappers enabled
	 */
	$ch = curl_init($service_url);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$output = curl_exec($ch);
	curl_close($ch);

	// return false on error
	return strtolower(substr($output,0,5))=='error' ? false : $output;

}
</pre>
<p>The function defaults to tinyurl. Yeah. Interesting choice you&#8217;re thinking, but I&#8217;m old school and that&#8217;s how I roll. I&#8217;ve used curl to make the HTTP requests for the simple fact that when it comes to hosting curl support is enabled more ofthen than fopen wrappers is on (and you need fopen wrappers on to be able to do a file_get_contents on an url). Either way the result is the same. Use whichever method you like.</p>
<div class="pullout">
<h2>Expanding Short URLs With PHP</h2>
<p>For expanding short urls on the fly with PHP see: <a href="http://tinyurl.com/php-expand-url">PHP expand_url function</a> </p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jamiethompson.co.uk/web/2010/05/18/create-short-urls-with-php-shorten_url-php-function/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHPMailer Bug: Headers showing in message body</title>
		<link>http://jamiethompson.co.uk/web/2009/07/01/phpmailer-bug-headers-showing-in-message-body/</link>
		<comments>http://jamiethompson.co.uk/web/2009/07/01/phpmailer-bug-headers-showing-in-message-body/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 17:00:51 +0000</pubDate>
		<dc:creator>Jamie Thompson</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jamazon.co.uk/?p=221</guid>
		<description><![CDATA[PHPMailer is a handy little class which smoothes out the minefiled that is sending email with PHP. Okay, it&#8217;s not exactly difficult, but it&#8217;s not always striaghtforward either.
I did come across a niggly little bug in the latest release (2.3 at time of writing) which causes a large number of mail clients to incorrectly parse [...]]]></description>
			<content:encoded><![CDATA[<p>PHPMailer is a handy little class which smoothes out the minefiled that is sending email with PHP. Okay, it&#8217;s not exactly difficult, but it&#8217;s not always striaghtforward either.</p>
<p>I did come across a niggly little bug in the latest release (2.3 at time of writing) which causes a large number of mail clients to incorrectly parse the generated email headers. The most obvious manifestation of this bug is that the headers are displayed in the body. Which probably isn&#8217;t what you want.</p>
<p>Fortunately the &#8216;fix&#8217; is simple. By default PHPMailer 2.3 defaults to using &#8220;\r\n&#8221; for linebreaks. This is what confuses a lot of mail clients as they&#8217;re expecting a &#8220;\n&#8221;.</p>
<h2>PHPMailer Class, line 259:</h2>
<p>change:</p>
<pre class="brush: php">var $LE = "\r\n";</pre>
<p>to:</p>
<pre class="brush: php">var $LE = "\n";</pre>
<p>That worked for me, although your mileage may vary.</p>
]]></content:encoded>
			<wfw:commentRss>http://jamiethompson.co.uk/web/2009/07/01/phpmailer-bug-headers-showing-in-message-body/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to Install cURL support under Uniform Server 3.5 Apollo</title>
		<link>http://jamiethompson.co.uk/web/2009/03/31/how-to-install-curl-support-under-uniform-server-35-apollo/</link>
		<comments>http://jamiethompson.co.uk/web/2009/03/31/how-to-install-curl-support-under-uniform-server-35-apollo/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 11:22:33 +0000</pubDate>
		<dc:creator>Jamie Thompson</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA["uniform server"]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[uniserver]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://jamazon.co.uk/?p=195</guid>
		<description><![CDATA[Uniform Server 3.5 Apollo is an excellent environment for those of us who, for whatever reason, use Windows for PHP development. Unforunately it comes without cURL support. After a bit of digging I figured out (i&#8217;m no linux sysadmin) how fairly simple it is to get cURL up and running with uniserver.
The following assumes that [...]]]></description>
			<content:encoded><![CDATA[<p>Uniform Server 3.5 Apollo is an excellent environment for those of us who, for whatever reason, use Windows for PHP development. Unforunately it comes without cURL support. After a bit of digging I figured out (i&#8217;m no linux sysadmin) how fairly simple it is to get cURL up and running with uniserver.</p>
<p>The following assumes that you have Uniform Server installed in &#8220;%ProgramFiles%\Uniform Server\&#8221;. If you have it somewhere different just modify the instuctions accordingly.</p>
<p>In the php.ini file located in &#8220;%ProgramFiles%\Uniform Server\udrive\usr\local\php&#8221;, search for the following line:</p>
<pre class="brush:plain">;extension=php_curl.dll</pre>
<p>and enable curl by removing the semicolon as follows</p>
<pre class="brush:plain">extension=php_curl.dll</pre>
<p>It&#8217;s not quite as simple as that. You&#8217;ll need three dll files for cURL to actually function. Luckily you can get them here.</p>
<p><a href="http://jamazon.co.uk/files/uniserver_curl.zip">http://jamazon.co.uk/files/uniserver_curl.zip</a></p>
<p>Download and unzip the file above. You should end up with three relevant looking dlls</p>
<p><strong>ssleay32.dll </strong><br />
copy into &#8220;%ProgramFiles%\Uniform Server\udrive\usr\local\apache2\bin&#8221;</p>
<p><strong>libeay32.dll</strong><br />
copy into &#8220;%ProgramFiles%\Uniform Server\udrive\usr\local\apache2\bin&#8221;</p>
<p><strong>php_curl.dll</strong><br />
copy into &#8220;%ProgramFiles%\Uniform Server\udrive\usr\local\php\extensions&#8221;</p>
<p>Then just start (or restart) Uniform Server for the changes to take effect.</p>
<p>You can take a look at the output of phpinfo() or run the following code to determine whether or not cURL is installed.</p>
<pre class="brush: php">&lt;?
if (!function_exists('curl_init')){
die('Sorry, cURL support is not installed');
}
echo "Yay! cURL support is Installed!";
?&gt;
</pre>
<p>Bob&#8217;s your uncle!</p>
]]></content:encoded>
			<wfw:commentRss>http://jamiethompson.co.uk/web/2009/03/31/how-to-install-curl-support-under-uniform-server-35-apollo/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>imagettfbbox() Could not find/open font</title>
		<link>http://jamiethompson.co.uk/web/2008/06/04/imagettfbbox-could-not-findopen-font/</link>
		<comments>http://jamiethompson.co.uk/web/2008/06/04/imagettfbbox-could-not-findopen-font/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 23:51:41 +0000</pubDate>
		<dc:creator>Jamie Thompson</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[Bug]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jamazon.co.uk/?p=76</guid>
		<description><![CDATA[This evening I noticed a problem with the script which generates the image headings for this site. It had stopped working. I hadn&#8217;t noticed until now because the process caches all previously generated images and as such it hadn&#8217;t actually run for a while.
The error
imagettfbbox() [function.imagettfbbox]: Could not find/open font
The specified font file existed and [...]]]></description>
			<content:encoded><![CDATA[<p>This evening I noticed a problem with the script which generates the image headings for this site. It had stopped working. I hadn&#8217;t noticed until now because the process caches all previously generated images and as such it hadn&#8217;t actually run for a while.</p>
<h2>The error</h2>
<p><strong>imagettfbbox() [function.imagettfbbox]: Could not find/open font</strong></p>
<p>The specified font file existed and was readable. I was in fact testing for this eventuality using is_readable() prior to the the imagettfbbox call. After some poking around and a bit of googling I narrowed the problem down to a bug.. or feature in PHP > 4.3.0</p>
<h2>The solution</h2>
<p>It&#8217;s a simple fix, but what worked for me was to pass imagettfbbox an absolute path to the font. This is with PHP 5.2.0. I&#8217;ve read of this working for lots of people using various versions of PHP from 4.3.0 onwards, but it also seems that this isn&#8217;t always the solution, so your mileage may vary. As usual, it probably depends on a magic combination of seven million random compile flags.</p>
<p>So to sum up. Using imagettfbbox with relative font paths in PHP >= 4.3.0 may result in an error. A solution seems to be to use absolute paths. If this isn&#8217;t the issue, it could be spaces in the filename? Your really shouldn&#8217;t put spaces in filenames, what are you a windows user?</p>
<p>Official bug report here: <a href="http://bugs.php.net/bug.php?id=22092">http://bugs.php.net/bug.php?id=22092</a></p>
<p><em>Sometimes* PHP just sucks</em></p>
<p>* all the time</p>
]]></content:encoded>
			<wfw:commentRss>http://jamiethompson.co.uk/web/2008/06/04/imagettfbbox-could-not-findopen-font/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

