PHP Is Like A Toilet

No, not in the way you think I mean.

I love PHP. Turns out, I love toilets too.

No, PHP isn't dead.

A toilet!?

Bear with me for a second.

A toilet—and more recently, a flush toilet—is an incredible invention. It's probably the single biggest advancement in human sanitation.

Why?

Because once you're done, you press the button, turn the handle or pull the chain and flush everything away. The cistern fills with fresh water and we're ready to go again.

Admittedly, this is probably as far as we want to take this analogy. But(t), it's fitting for one important feature of PHP that makes it an ideal language for the web.

A fresh start

Most language runtimes, whether it's for a compiled or interpreted language, have some concept of maintaining state.

That means it's possible without any external service to store something in memory that your application can access across multiple invocations.

Technically, PHP has this capacity too, for extensions. This is what Opcache and APCu use to make your applications faster, for example.

But your average PHP application can't tap into this.

So, in typical use, PHP doesn't maintain any state between executions.

Everything gets flushed away at the end.

And this is great!

I think this is my favorite feature of PHP.

Why?

Because it means you're always starting with a clean bowl. And we all know what it's like when you don't start with a fresh bowl!

Ok, no more toilet analogy.

Clean starts mean that there's a much lower chance of a memory leak.

Clean starts are like the scientific method: reducing external factors that could affect your experiment.

Clean starts are easy to understand and remember.

Clean starts are like how serverless functions (e.g. AWS Lambda) execute: stateless.

Isn't this a 'cold start'?

Yes, we call them cold starts normally, borrowing terminology from engineering—like an engine that is starting after a period of no activity.

Like a cold toilet seat. And everyone hates a cold toilet seat.

So this means you have to build everything up from scratch on each request—warming it up—and this comes with a performance penalty.

Especially for pieces of your application that don't depend on user input and don't vary often, processing them every time could be wasteful.

As with many things, it's a tradeoff between performance and resource management.

Sometimes, you do need something to keep floating around to help speed up your application or reduce load on your servers.

Common solutions to help persist data between requests outside of PHP include:

  • Storing files to the disk or serialising variables/objects into a database (which will store it on disk)
  • Using an in-memory cache solution like a temporary table, Swoole, Redis, or Memcache
  • Using an application manager like RoadRunner or FrankenPHP to keep your application booted (via Laravel Octane)

At different points and in different contexts, these can all improve performance at the cost of some resources (memory/CPU/disk).

Most applications will need one form of persistence or another, but I think it's great that PHP defaults to starting from scratch every time because it's such an easy thing to comprehend.

Was this maybe the worst analogy ever?

Yes, but did you learn something? Did you laugh a little?

Well then, my job here is done.

*flush*