Why I don't like PHP syntax

Read this article in:
Body

Revised and updated 2026-07-25. 

I have made the decision to use PHP as part of my overall stack, along with ruby, javascript, and python. The main reason is that there has been a lot of great software written in PHP, and I want to use it. However, after leaving the PHP world for Ruby, and then coming back to it, I see many annoying little things that bother me in PHP. In this article, I rant about some of them.

But of note, PHP is nowhere near as ugly as typescript. And PHP is an old language, born before the modern internet, and designed for light usage aka "pages". Not for building behemoth monoliths. So at least PHP has this excuse for getting the syntax wrong. Typescript, on the other hand, was allegedly created when the modern internet already existed. There is no excuse for typescript to be as mind-numbingly ugly as it is. It's very sad news, everyone. I'm afraid there will be an entire generation of programmers who think ugly languages are normal. End mini-rant.

And now for the things I don't like about PHP.

ugly loops

If I write:

 foreach ($collection as $block => $region) { ... }

Notice the '=>' token. You would ordinarily see it in dictionary key-value assignment, but here it means the opposite, destsructure rather than assignment. Does it make sense to use the same token for it? I think no.

Also notice that when reading the above line, you have to read the middle of it, then go back to reading the beginning. It doesn't read: foreach ($block as $region in $collection) {...} . If I just read the first half and stop, the info is meaningless: foreach ($collection... Maybe php people are used to this abomination, but I am not, and I am hereby pointing out that it is bad syntax.

Compare that to python:

 for key, value in my_dict.items(): print(key, value)

Compare that to ruby:

 collection.map { |key, value| print(key, value) }

Elegant, right? Elegant in each language other than php.

Atricious namespace separator

What does this mean?!

 use Drupal\Core\Config\ConfigFactoryInterface;
 use Drupal\Core\Entity\Entity\EntityFormDisplay;

Oh, the backwards slash is... just an ordinary character all by itself. That's stupid, I saw that and I went back to not using php for a year. In the real world, in linux, the backwards slash escapes the next character. The backwards slash is never alone, and the following character is also a special character! You can escape special characters like newlines and dollar signs, you can escape things that don't exist, you can escape half a thing that yet doesn't exist - and it will be future-proof and portable. I'm talking about unicode: you can write \uXXXX and encode a portion of a multi-byte character that hasn't been added to a charset, all using the tremendous power of the backslash.

In a way, the backslash is a meta-meta character, it is more special than a backtick or a dollar sign, it is arguably the most special character of all. And PHP uses it... for what? For nothing, just to separate things. Separate things with a comma.

In fact, in the real world of linux, the backwards slash would escape the next character, and eg `Drupal\Core` would become just `DrupalCore`, since escaping `C` is a no-op and results in the same `C`.

Compare that to ruby:

 class Very::Simple::Namespace {}

compare to python:

 from utils.text.unicode import encode

Again, the only ugly one is php.

Stupefying array syntax

What in the world is this?!

 $person = [
     'name' => 'Alice',
     'age' => 30,
     'city' => 'Austin',
 ];

Square brackets?! But it's not a list. Everyone in the world other than these guys use `[]` to represent lists and `{}` to represent sets/dictionaries/hashes. Don't get me started...

Compare to ruby:

 person = {
   name: 'Alice',
 }
 person = {
   'name' => 'Alice',
 }

compare to python:

 person = {
     "name": "Alice",
     "age": 30,
     "city": "Austin",
 }

Again, PHP is the ugly one out.

Semicolons are required?!

Neither in Ruby nor in Javascript are semicolons required. Nor in Python. What do you need a semicolon for? And before you say javascript requires... no it doesn't. And I skip the typescript because it is just chaotic. I remove semicolons from my javascript code as a matter of policy. You know when a statement ends because... I don't even need to explain it, every reasonable modern language just figures it out. Semicolons are a thing of the past and should be optional. Like winding keys for wrist watches: they are no longer needed.

~ * ~ * ~ * ~

I'll update the list once I see or remember other annoying things. But for now - I hope you can see how people have strong language preferences. And this was just simple syntax, not reflection, not standard library or other considerations. I think ruby is an infinitely more elegant and pleasureful language, and it is upsetting that ruby is not getting enough use from the world's programmers.

 

Please login to post comments: