5 Nice things to say about PHP
5 Nice things to say about PHP
We recently had to install WordPress on a Windows box at work. A lot of bad words were said. Worse, as I write those line we are still not done. PHP is giving us a really hard time, with the MySQL extension not loading. I remembered the convoluted way PHP error handling work: "Well, see, sometime PHP throw an Exception, but sometimes it raise an error, but you can silence it with @, or you can attach an error_handler and..." One of my coworker is convinced that PHP is the most horrible language ever invented, and I can't blame him. PHP is full of glaring flaws and bizarre way of working. Loathing is common against PHP.
However, I believe strongly that considering something to be bad with no redeeming qualities is wrong. I also hate insulting a whole community of programmers just because I dislike their tool of choice. If PHP was rotten to the core, nobody would use it, and another language would have powered most of the Internet in the 00s. If Facebook, WordPress and Wikipedia are running on PHP, surely the language must do some things right?
So while I wouldn't willingly choose to code in PHP today, here are 5 nice things I can say about it.
But before, full disclosure: PHP and me, we go waaaaaay back. This is not a rant by someone who never used the language. PHP was the first programming language I ever learned and became pretty good at. I once knew its documentation in great details, and had memorized what most settings in php.ini file did. It was with PHP, at the age of 16, that I created my first ever software, a CMS. I actually delivered in 'production' -one of my friend used it for its personnal website-. It's with PHP that I had my first real programming job. I was 20-21 at the time, and I had just started university. I have great memory of working with PHP and having a lot of fun using it.
Which bring me to my first point:
1. PHP is a fun language to use when you are learning to code
I mean it! PHP feels like a logical next step from raw HTML files when you are learning to create websites. Take a .html file, rename it to .php, and, just like that, you can start coding. Well, assuming you have a webserver with PHP installed, but a quick WAMP install and you are done. I can still remember the first time I typed the following in notepad++.
<html> <body> <p> <?php echo "Hello from PHP!"; ?> </p> </body> </html>
Boy did it feel like I could take over the world with this simple 'hello'.
This is what is great about PHP. C or Java require you to learn a lot about programming before you can use them. But in PHP you can create something useful even as a beginner. Just with a couple of variables,
if and while, your website can come alive. Slap a few HTML form and a MySQL database on top of it, and you can get a very basic form of CMS going, and display interactive things. You don't need to learn about pointer arithmetic or Object-Oriented programming. You can get going with just the basic.
And in addition to being simple, PHP also protect you from whole classes of error. Speaking of which:
2. PHP Make a lot of things safe by default
In PHP, it's hard to leak connections, memory, or anything. Even if you forget to call
mysql_close at the end of your page, your connection will still be closed automatically once the script terminate. Since the whole process is brought down at the end of the request, all memory and resource are released. Even infinite loops will be terminated by the runtime if the script exceed a time limit. For a beginner, this is great, because you are protected against yourself. You can make many mistakes and still have a functional website.
That protection also mean that it is hard for PHP to bring a server to its knee. It's easy for a Web hosting company to let users use PHP, since the language come with built-in protection from abuse. That safety created a whole industry of cheap hosting for PHP.
And this is my next point.
3. PHP is incredibly easy to deploy
Take any cheap host. Upload you PHP files via FTP. Create your database in PhpMyAdmin. There you go, you have your dynamic website up and running. This is incredibly easy. In the 00s, this was amazing, orders of magnitude easier than ASP.NET or Java, and significantly cheaper that a classic ASP host. This is helped a lot by PHP "process by request" and "file per resource" approach.
Today, PaaS service like Heroku make it quite easy to deploy a web application. But even now, I have yet to see something that is as beginner friendly as opening FileZilla and uploading a bunch of files to a server. With PHP, you don't have to grasps concepts that experimented programmers take for granted but are gibberish for beginner. You don't have to know the command-lines, or how to use git, or how to create a build script. Those things are hard the first time you do them. And the documentation of those tools is often intimidating.
But speaking of documentation...
4. PHP has nice and beginner friendly documentation
OK, a total noob won't learn to code with the PHP documentation. But once you have the general idea, the PHP documentation is actually pretty well made. Functions are explained with a lot of code example. You can read the PHP API documentation page by page and learn interesting things. And each module has an introduction with a lot of examples. This is a great when you want to have a 'general idea' of what a group of function and class does. Reading the JavaDoc of a library is quite painful in comparison.
And unlike Java...
5. PHP development can be done easily anywhere
PHP doesn't assume a UNIX environment when you develop. It can be done without any command-line or special IDE. It doesn't require a Professional version of Windows that can run IIS. It doesn't require hard to install servlet container or something of the like. You don't need to setup anything in your PATH. No, just download WAMP or something equivalent, fire Notepad++ and get going!
Setting up MySQL is almost as easy. Fire up PhpMyAdmin to create your tables in a visual way, insert a few rows, and there you go. And PhpMyAdmin is incredibly useful to any beginner that don't understand database well. The MySQL GUI tool for creating tables were quite hard to use in comparison.
All of this combined mean a teenager on his parent computer can download all he need to get started and have fun learning to code. Those tools sure aren't as powerful as with other languages, but when you get started, its the simplicity that matter more.
Having said that...
Despite all those good word, I would avoid PHP today unless I had a very compelling case for using it. I grew terribly tired of all its shenanigans. Since I have tasted languages with more consistency and more professional APIs, like Java, C#, python, etc., I really don't want to go back.
I won't repeat all the issues with PHP, they are well known. Suffice to say that at this point in my career, the weakness of PHP frustrate me much more than its strength help me. I am not a teenager making personal web site anymore, I am a professional software developer dealing with difficult business logic. PHP does not help me for that like other language do.
Still, PHP has its merits. Yes, it suffer from all the flaw of a beginner language. Yes, the protection it give you mean you won't have the flexibility you may need. Yes, the many unexperienced developers using it mean you are quite likely to find crappy PHP code in the wild, and yes its design flaws drive professional away so you won't have a lot of great code to compensate. And yes, once the initial fun of making web page alive start to dissipate, the PHP shortcomings become quite crippling.
But PHP was the starting point of a lot of programmers career. A lot of on-line communities are backed with PHP code written by passionate people. A lot of big commercial websites are powered by PHP. Like VB6 or Excel Macros, PHP has solved many problems for many people. So while I curse PHP under my breath while trying to get that annoying WordPress install to work, I still recognize that PHP empowered many beginners to create their own software, and ran a vast chunk of the Internet for a long time. This is something I can respect.
Well, so long PHP, old pal. I hope you will forgive for all the bad thing I say about you.