‘Prepare’: PHP’s simplicity adapted for today’s web

Nardi Lam
4 min readDec 26, 2014

--

PHP has historically been the most used and the most criticised language for web development. It is horribly inconsistent, not very performant, and enables tons of dangerous programming practices. It is also very unsuited to the current trend in web development, where “web apps” have become just as complex as traditional desktop apps, both on the server and client-side. PHP is intrinsically bound to the Web 1.0 model of HTML preprocessors, and many alternatives which are better designed for this kind of application have sprung up.
However, despite all this, PHP is still very much relevant. I would postulate this is because of two main reasons:

  1. There are very large legacy codebases written in PHP that are still in use (e.g. WordPress, and internally at places like Facebook).
  2. It’s simple to understand.

Modern web application frameworks such as Ruby on Rails, Django, or client-side ones such as AngularJS solve many problems encountered when building a modern web application. PHP solves none of these. However, that is actually one of it’s strong suits. When learing to understand “how websites work”, after getting down some basic HTML and CSS, PHP is a logical next step in the process: it’s time to incorporate a server to (usually) have some sort of persistent data store, and PHP lets you add this to your existing pages that you know how to make in an easily understandable way. No serious web developer continues writing PHP their whole life (except for maybe a quick script once or twice), but almost everyone starts there. And this is probably a good thing: it’s good to first understand a simpler solution and its flaws before moving on to a more complex, but better one.

So the problem is not that there exists nothing better than PHP, it’s that there is nothing that really competes with it on it’s strong points. That means there still is a place for something that uses the same simple methodology of preprocessing web pages, but does not make you use such a badly designed language, and (hopefully) incorporates some features that make it easier to eventually transition to more appropriate solutions for writing web applications.
Prepare.js was created to try and fill this gap. Prepare is an HTML preprocessor like PHP, but instead allows you to embed JavaScript snippets or files in your HTML and have these be executed server-side, before the page is delivered to the client. This alone has a number of advantages over PHP:

  • Same language on server and client — no need to learn a (practically useless) language like PHP first
  • Natural extension if the user already has knowledge of how client-side Javascript works
  • Otherwise, the user learns JavaScript, which is also a very useful skill outside of Prepare itself
  • Allows you to reuse client-side idioms for things like modifying the DOM
  • Can tap into the large number of client-side JavaScript libraries AND tons of npm modules

On top of that, you might find some traditionally more complex things are actually very easy to accomplish because of the lack of language and environment barrier, and strong coupling between client and server-side code. Now, this does not mean Prepare is suited for building any kind of large-scale application, but this is mostly because of the lack of structure: I could imagine it possible to build some kind of production-ready framework on top of it. But for personal, small-scale, and educational use, it’s for example very easy to build an implementation of Comet (and I mostly picked this because that was one of the first things I did) and use it for something like a real-time chat room (I recall that being quite a popular type of example app for these kind of things, heheh).

So that is basically what Prepare is. While I also find it an interesting (and kind of fun, actually) way to approach web development, the main reason I think it actually has value is because of the teaching angle. Even in the first (and only) course in web development in my bachelor Computer Science program we were taught to use PHP, and I honestly think something like this would be a better alternative. Sure, PHP might have a bit more leverage on account of being an “established” platform, but really, it shouldn’t be: it should be a temporary playground you try to get out of as soon as possible, and Prepare could fill this same role and leave you better equipped when the time comes.

I would very much like to hear what you think.

https://github.com/nardi/preparejs

PS. Another reason for PHP’s popularity could be it’s availabilty: every cheap (or free) shared hosting provider allows you to run PHP apps. Prepare doesn’t have this (cheap hosts generally don’t support Node), but even without official support Prepare’s preprocessor nature means that it can be ran in the same way as PHP: start up a process to handle one request and then shut it down. So unless you count on some variables being persistent between page loads in your app (an in-memory session for example), you should be able to run Prepare on a shared host if you want to (with a reasonable performance penalty of course, though you’ll still probably beat PHP…).

--

--