PHP 5.4 and Short Tags

PHP 5.4 isn’t exactly new; in fact the opposite is true! PHP 5.4 is end of life, but as our adoption rates show, as a community, PHP people aren’t especially good at upgrading! I’m getting lots of questions now because some of the hosting providers, notably including Acquia’s hosting, are finally upgrading away from those 5.2 and 5.3 offerings.

One thing in particular is tripping people up: the short open tag. I’ve had a few questions on this so here’s the advice I am giving to clients and friends.

What Actually Changed

The short_open_tag configuration directive was removed, but the short echo syntax <?= is always available.

How To Upgrade Your Codebase

  • If you have <?= in your templates, leave it alone, those will still work
  • If you have short tags <? in your code, including in any of your libraries, then you need to do a global find-and-replace and turn them all into <?php

If you have short tags somewhere in your codebase, you probably won’t get errors, you’ll just suddenly start seeing PHP code in your output as PHP doesn’t recognise the tag and therefore doesn’t evaluate the code! To find them, try searching for <? followed by a whitespace character.

Hopefully that helps; there are a few gotchas to getting upgraded from older versions (especially from PHP 5.2) but this particular gotcha really isn’t a problem and the instructions here should see you through.

PHP Version Adoption

PHP runs over 75% of all websites whose technologies are known (source: w3techs), which makes for a really REALLY long tail of users who once installed wordpress, phpmyadmin, or some other open source project that helped their business needs at the time. What they don’t do is upgrade. PHP’s current usage statistics look like this (source and raw numbers are if you want them):

PHP Version Adoption

What’s alarming about this is that the left half of this graph represents unsupported versions of PHP. PHP 5.2 has been end of life since January 2011. This doesn’t mean that you can’t use it any more, but it does mean that in terms of security updates, you are out of luck. Some distributions will try to retro-fit some of the fixes but essentially your PHP applications seem a bit lacklustre because, well, you’re using technology from 2006. Continue reading

Zend Webinar on PHP 5.4: 19th February

I am pleased to announce that I’ll be presenting one of Zend’s webinars in February, on Tuesday 19th. The topic is “The PHP 5.4 Features You’ll Actually Use”, and the session is my opportunity to round up the best of the new features that came in with PHP 5.4 and illustrate the ones you’ll want in your codebase with some examples. You can find out more about the session and register for the webinar on Zend’s site: http://www.zend.com/en/company/news/event/1188_webinar-the-php-5-4-features-you-will-actually-use.

See you there!

PHP 5.4 Timezone Error Message on Ubuntu

Quick post because this tripped me up the other day: When you use a vanilla ubuntu 12.10 “Quantal Quetzal” installation, it will come with PHP 5.4, which is excellent news. However the default php.ini doesn’t set the timezone, so you will see an error like:

It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier.

These have been warnings in earlier versions of PHP, but as of PHP 5.4, the date.timezone ini setting must be set correctly, using the continent and place – for me that’s “Europe/London”, like this:

date.timezone = "Europe/London"

If you see these errors, don’t panic, just add the line above to your php.ini.

Managing PHP 5.4 Extensions on Ubuntu

My shiny new VPS* runs Ubuntu 12.10 (official subtitle: Quantal Queztal. Local nickname: Quirky Kestrel) and therefore has PHP 5.4 installed. It’s very new so every command I type is missing, and today I realised that included a PECL module (pecl_http, of course). So I aptitude install php5-pear and then get tangled in dev packages (clue: look which libcurl you have already installed to figure out which of a long list of -dev packages to choose), managing finally to emerge with a pecl install http that completes successfully with the words:

configuration option "php_ini" is not set to php.ini location
You should add "extension=http.so" to php.ini

I’ve been using Ubuntu for some time however, and we don’t put settings straight into php.ini, there’s a directory called /etc/php5/conf.d/ where all the various module configurations live, or you can enable things just for when PHP is called by apache or from the CLI. However today I hopped into /etc/php5/ and saw this:

.
├── apache2
├── cli
├── conf.d
└── mods-available

Hmmm … mods-available ? Continue reading

PHP 5.4 Benchmarks

Today I’m giving my first ever talk at OSCON – about PHP 5.4 (I’ll also be giving my second ever talk at OSCON, about RESTful services; it’s a busy day!). My talk includes some benchmarks which I thought I’d also share here, mostly because I like pretty graphs – and this one is pretty:

graph comparing performance of PHP versions
Continue reading

Proof that PHP 5.4 is Twice as Fast as PHP 5.3

So recently I was working on some benchmarks for different versions of PHP, because I heard that PHP 5.4 is “faster” and since I’m a data geek I want to know how much faster! Now, PHP 5.4 is, in general, faster than PHP 5.3 but not twice as fast* unless you pick a use case which has been particularly optimised.

My first attempt at benchmarking the two versions produced this:

graph showing php 5.4 taking half the time of php 5.3
Continue reading

PHP 5.4 Built In Webserver

One of the big features arriving with PHP 5.4 is the addition of a built-in basic webserver for use in development environments. Quite a few of the other scripting languages have something like this so I’m very pleased to see it in PHP. Using a server like this makes it easy to quickly try out some scripts without needing to configure apache or really do anything much! I had to look up a few things to get started, so I thought I’d write them down for posterity. Continue reading