Writing Readable PHP
I still remember coding up my first bits of PHP around the beginning of the century. That makes me sound very old, but let me assure you that I'm still young at heart. My coding was done in Notepad, without any code highlighting or autocompletion. The thing I remember most is the magical feeling that I managed to output some HTML in the browser using PHP.
The code itself, how it was written and structured, didn't feel too important to me. What was important was that the code did what I intended: rendering a page, and that's that. I would write a lot of code but rarely reread it. This became problematic after a while: updating existing projects became cumbersome because I couldn't understand my code.
Nowadays at Spatie, things have vastly changed. Coding is not done on my own but with an entire team. Now I believe that how you accomplish something is as important as the accomplishment itself. This is probably true for most things in life. But let's not get into old-mans-talk-territory and stick to code. How the code is written is at least as important as what the code does. At Spatie we often say that if you write code that works, you're only halfway there.
You might already have heard that code is read a lot more often than it is written. Through experience, we know that is true.
Over the years, our team and I have learned a lot of patterns, conventions and tips that make code more readable. Most of these things are very small, but using all those tips together makes an incredible difference in code readability. Together with Christoph Rumpel, a fantastic teacher and programmer in his own right, we put all of our knowledge on code readability in a course.
You should consider this course a living document. We will add and change things in the content to reflect our current stance on things. These tips work well for us, but you don’t have to blindly follow or agree on them all. Under each section, you'll find a comment box. We welcome your thoughts about and additions to our tips. This way, you can also help us create the best course on writing readable PHP.
Enjoy!
Freek