Bummer PHP

Posted on 20.12.2015 by Kim N. Lesmer.
Since the advent of the PHP programming language, PHP has been both loved and hated by many. In this article we'll take a look at one of the reasons why PHP really is such "a bummer".

A programming language is only as good as it's interpreter or compiler.

The interpreter or compiler is the software that parses the source code of the programming language and performs its behavior. Whether the source code is compiled into machine language or translated into an intermediate representation and executed is not important, what's important is how well the interpreter or compiler does its job and how well the interpreter or compiler handles errors in the source code.

Without going into a lengthy discussion about the PHP interpreter, lets just look at one single example of how it handles a specific source code error. This example can serve as a demonstration on just one of the many design flaws of PHP.

Don't get me wrong, with PHP you can be really productive and produce software quickly and I like PHP, but I have also used it since about the time when Rasmus Lerdorf originally released it and as such - over the years - has gotten to know the "quarks and quirks" of the PHP interpreter. However, it should be known that PHP was never designed, it was - and still is - mostly a "hack".

A hack is an inelegant solution to a problem. In this sense, a hack gets the job done but in an inefficient, un-optimal or ugly way.

-- Techopedia.

Lets look at some examples of the problems with the PHP interpreter.

This is just a really simple piece of code that only serves as a demonstration!

01. $string = "Welcome to my personal home page;
03. if ($page_id == 0) {
04.    renderHomePage();
05. } elseif ($page_id == 1) {
06.    renderContactsPage();
07. } elseif ($page_id == 2) {
08.    renderAboutPage();
09. } elseif ($page_id == 3) {
10.    renderServicesPage();
11. } elseif ($page_id == 4) {
12.    renderWeatherPage();
13. } elseif ($page_id == 5) {
14.    renderNewsPage();
15. }
17. echo "Thank you for visiting!";

Can you spot the error?

Now, if we run this through the PHP interpreter we get the following error messages:

In PHP 5.2, 5.4 and 5.5:

FATAL ERROR syntax error, unexpected 'Thank' (T_STRING) on line number 18

In PHP 5.3:

FATAL ERROR syntax error, unexpected T_STRING on line number 18

In PHP 5.6 and 7.0.1:

PHP Parse error:  syntax error, unexpected 'Thank' (T_STRING) on line 18

PHP 7.0.1 was released only a few days ago (17 Dec 2015)!

The problem with this error message is that the error is on line 2 (a missing ending double-quote), not on line 18.

$string = "Welcome to my personal home page;

But what really shows how horrible the interpreter handles this is that PHP uses semi-colon to end expressions. The interpreter uses the semi-colons to know when an expression has been terminated, yet in this example the interpreter still runs through the rest of the code all the way down to the last line and serves that line as "the problem".

You might argue that most modern IDE's and editors would catch this error and color the lines after the missing quotes and as such this error would be discovered before the code is run, but this argument only serves to prove that even your IDE is better at catching this error than the PHP interpreter itself!

Lets reproduce the error in a couple of other programming languages and see how their interpreters or compilers handles the error.

In the following examples I have shortened the code output for brevity.

Golang (or just Go)

1. package main
3. func main() {
4.     var welcome = "Welcome to my personal home page
6.     if ($page_id == 0) {

Compiled with the Go compiler we get the following error message:

test.go:4: newline in string

Very simple and precise. The error is located on line 4 (in this case) and the problem is a string with a newline.

If we use the GNU Go compiler (gccgo) we get the following error message:

test.go:4:49: error: unterminated string
var welcome = "Welcome to my personal home page

Extremely helpful and precise. "Unterminated string" at line 4 at the 49'th character and then we even have a pointer ^ showing us where the problem is located.


1. class helloworld  {
3.     public static void main(String[] args) {
4.         string welcome = "Welcome to my home page;

Lets have a look at the error message:

helloworld.java:4: error: unclosed string literal
string welcome = "Welcome to my home page;
1 error

Again we get a very helpful error message that tells use the correct line number and what the problem is with a nice pointer.


1. welcome = "Welcome to my home page
3. if page_id:

And the error message from the Python compiler (2.7.9 in this example):

File "test.py", line 1
welcome = "Welcome to my home page
SyntaxError: EOL while scanning string literal

Again we get the correct line number and a good explanation with a pointer. "SyntaxError:" and "EOL" meaning "End Of Line".


1. $welcome = "Welcome to my home page
3. if page_id == 0

And the error message:

test.rb:1: unterminated string meets end of file

The Ruby interpreter also provides us with the correct line number for this case and a very precise explanation of the problem.

We can find strange error messages that makes absolutely no sense in many programming languages, but the point is not about eloquence. In this example the PHP interpreter - going back all the way to 5.2 and further - makes absolutely no sense and is even providing the wrong line number.

Every PHP programmer is familiar with the day-to-day tasks that can be tricky or cumbersome.

-- Facebook developers Julien Verlaguet and Alok Menghrajani.

I personally don't like having my editor insert quotes automatically so I always write both the start-quote and end-quote before going back to write what goes inbetween the quotes, but before I got into that habit this PHP problem had me staring at the PHP source code for a long time. Eventually you'll find the error either by commenting out pieces of code until it works or just by browsing through the code, but this is a really good example on how bad PHP is.

IMHO the really bad things in PHP and the core functionality of the language should have been fixed long ago instead of the constant adding of new features. The PHP developers keeps adding new functionality, new paradigms, and more complexity to the language (making it resemble Java more and more) and the language keeps growing everytime a new release is made.

Of course the developers have also fixed a lot of bad stuff over the years, anyone in doubt can just take a look at the changelogs, but adding new functionality and new paradigms to a language that still can't get basic error messaging right - amongst a lot of other things - is really bad.

PHP is indeed used by many major websites around the world, but PHP still is - and has been - one of the major reasons for software going really bad on the Internet.

Recommended reading:

phpsadness.com (Also contains the example used above).

If you have any comments or corrections feel free to email them to me.