Stop Using These 5 Outdated Optimization Tricks in PHP

Vlad Reshetylo
5 min readFeb 29, 2024

Premature optimization is the root of all evil

Donald Knuth.

We all heard this quote. Maybe even dozens of times. But at the same time, many developers continue using some old-fashioned tricks.

Why and what’s wrong? Let’s figure it out 🙂

It seems for me that love for “performance tricks” often happens with developers with 10+ years of experience. They know a lot of tricks, and they have thoughts like “it will not be worse” and “there is never too much performance.” Another case — is developers who want to show their skills. “Ha-ha- look at the trick that I know.”

And as a result, we have a weird code like this:

Why “sizeof” instead of “count”? Why not a “foreach”? What the hell with “echo”? Isset of string index???

All of that is about “performance”. At least, we were thinking that ten years ago. And in reality, there was no big sense to use such tricks even ten years ago. Yeah, some weird stuff could give you some small performance wins, but:

  1. If you really care about nanoseconds — you use the wrong tool (PHP)
  2. There is a 99.99% chance that the performance bottleneck of your code is not the loop syntax or the double quotes instead of a single

Still not convinced? Okay, what if I told you that some old-fashioned tips make no sense any more? 🫨

I picked 5 of the most common tricks known for every developer working with PHP 5.6 and older. All of them have no sense in PHP 8.x+

1. Using isset() instead of strlen()

It’s an excellent example of idiomatic code. Instead of checking the actual length of the string, we were proposed just to check the existence of the N-th symbol in the string.

Weird. But works. But weird.

And it really had some difference in performance in PHP 5.6. My local tests show around a 40% boost.

But… it doesn’t make sense anymore after the release of the PHP 7.0. Strings processing was greatly improved, and now the speed is the same.

I didn’t manage to measure any difference even on a set of 500K lines.

2. Using a “For” loop instead of a “Foreach”

It’s simple — we know that the “for” loop is faster than the “foreach” loop.

Okay, technically — it still makes sense. Yes, a “For” loop is still faster.

But:

  • A “Foreach” loop was greatly improved, and now overhead is much smaller than years and years ago
  • It makes no sense to use that until you have hundreds of thousands of items in your array

So yes, this stuff works, but you really don’t have to use this trick until you need to loop through a massive pile of data. Otherwise, you will just make your code less readable without a good reason. 🤷‍♂️

3. Using single quotes instead of double

The logic was really simple — PHP handles single-quoted strings as simple strings and processes double-quoted strings as template strings that can contain variables. And the idea was simple — use double-quoted strings only if you want to pass a variable inside. 🌚

To be honest, even at that time, I doubted this trick. 🫠 Seriously, I have no idea what the hell you should code to have a real impact of this thing.

But now it officially has no sense. Strings processing was improved a lot in the PHP 7.0 release, and now they are incredibly fast. Feel free just use double quotes everywhere. Seriously, stop bothering yourself with these quote marks 🥹

4. Using concatenation instead of template strings

The idea was quite similar to the previous point. Template strings were slower than simple strings, so developers decided it was quicker to concatenate strings instead of using templates (why? 🤣). Even if it looks uglier than template strings.

🤓

The point is the same — doing that after PHP 7+ makes no sense. I will say more — contributor of PHP engine says that concatenation can be even slower than string templates in modern PHP.

5. Using prefix increment instead of postfix

🤣

For me, it looks stupid. Seriously, come on, there is no way this stuff will give you any kind of significant performance improvement. But I saw it too often in a real code to doubt that people believe in that.

Yes, there was a tiny difference in speed 10 years ago, but Zend Engine constantly improving, and now the difference is smaller than the level of measurement error, even when you have millions of operations (I wish I saw the PHP code that really needs to do incrementation millions of times).

To summarise, stop using Micro-Optimization Tips in PHP, sacrificing code readability for imaginary performance improvements. Moreover, stop teaching these tricks to beginners. 🥹 It does not make sense.

Some of these tricks simply don’t work, and some work but will give you minus 0.001 microseconds in script execution. It’s just pointless 🙂 One SQL query that didn’t hit the index will destroy all these “improvements.” And vice versa, the well-used cache can give you seconds of difference instead of microseconds.

Do you know other pointless “optimizations” that just ruin the code readability? Share them in the comments, it’s really interesting to hear about them 🙂

--

--