There, I said it.
I know it’s fashionable to mock PHP for its antiquated syntax and semantic quirks, but I just like it. Here is why.
PHP is like C
This is really the main point of this post, and it’s a realization so simple that I’m surprised not more people made it. I have often observed that developers mocking PHP tend to be much more positive when I ask them about C.
“C is alright, it’s not OO but it was designed with a simple goal in mind and it does that very well. It’s straightforward and very well documented”.
Does that remind you of another language? That’s right, PHP! PHP is exactly like C. Either you like both or you don’t like either, there is no claim you can make about PHP that can’t be made about C as well, and vice versa. PHP was created with one very simple goal in mind: enabling the insertion of programming logic in web pages. And it does this fantastically well.
The absence of OO functionalities in both languages is a bit of a bummer (PHP 5 tried to address that with mixed success), but with some discipline, it’s really not hard to reach a reasonable architecture for your programs.
PHP never let me down
I write PHP code very sporadically, whenever I need to update one of my web sites or when I need to put together a small piece of web functionality that requires some programming. I am familiar with the PHP syntax but I don’t know much of its API, because I use it so rarely, so I pretty much need to relearn it from scratch each time. Whenever I need to get something done, I spend a decent amount of that time looking up docs on Google. And PHP absolutely shines in this area. It’s not just that looking up a function name will give you its API documentation as a first hit, but you can literally type what you need in English (e.g “most recent file in PHP”) and it’s very likely that you will find how to do it in just a few clicks.
You can even misspell function names (see for example the result of my incorrect query for strtime) and you will still land in the right place.
PHP is robust
I know it sounds silly considering how primitive and old the language is, but the bottom line is that code that I wrote more than ten years ago has been working absolutely flawlessly and without any changes for all that time. I don’t even bother writing tests for most of the PHP I write (obviously, I would be a bit more thorough if this code were destined to be used in a more mission critical web site). Not writing tests is not the only software taboo that I break when I write PHP: I happily mix up presentation and logic all the time. That’s just how PHP is supposed to work, and when the site you are working on is low volume and only of interest to a very tiny fraction of users, you probably don’t want to spend too much time on tasks that look overkill.
PHP’s documentation is great
It’s hard to pinpoint what exactly makes PHP’s online documentation so useful. It’s probably a mix of the content, the syntactically colored code samples, the CSS and most of all, the examples at the bottom. These are absolutely priceless and I don’t understand why not all API documentations do this.
Whether you are looking up an API function or a UNIX command, the first thing you want to see is examples, not a laundry list of its options and switches or a formal definition of its parameters. Very often, reading the example is all you need to carry on with your work and you can always read the more formal documentation if you want to use the function in a more advanced manner.
Universal support
Most Internet providers offer PHP support out of the box, so you don’t need to resort to more expensive VPS providers. I have yet to see this kind of universal support for any other language than PHP. Not even Ruby on Rails, let alone Java, is available on mainstream providers, thereby validating the claim I made five years ago that Ruby on Rails won’t become mainstream (I regularly receive emails about this article asking me this question, and I keep responding “Nope, still not mainstream”).
High reward
There is nothing more exciting than modifying a file, hitting Refresh on your browser and seeing the result right away. This brings me back to the very first days that I started experimenting with a web browser, more than fifteen years ago. Modifying an HTML file and seeing the result almost instantly hasn’t lost its appeal, and PHP is certainly continuing the tradition.
Sometimes, I don’t even bother editing the files locally and then transferring them: I ssh to my server and modify the files live. If something goes wrong, git makes sure that I can always back up my changes very easily. By the way, the combination of git’s branches and PHP is very powerful, allowing you to switch between entire web sites with just one command.
Conclusion
Even though writing in PHP always feels like going back in time, I’m never reluctant to doing it because I know that it will be rewarding and relatively easy. PHP has by far the highest “Get in, code, get out” factor that I have found in a language, and until another language comes around that can do better on this scale, I will be using PHP for many more years to come.
Update: Discussion on Hacker News, reddit and Google+
Pingback: Regarding “I Like PHP”… | Keritas
#1 by Ali on August 11, 2011 - 6:33 am
Quote
I’ve used php for several years. It is very easy and comfortable. There is a lot of applicable free sources in the internet written by php.
But recently I had to implement encryption on some data from dot net. I searched a lot for encryption methods in php. But It was very disappointing. There isn’t perfect implementation for cryptography. I couldn’t solve padding problem at last. I used dot net in this regard.
In case of data encryption php lets me down!!
#2 by Justin on August 11, 2011 - 7:01 am
Quote
Cedric said “Fair enough, Justin, but one advantage of PHP over .net is that it’s multi-platform. I don’t see how I could easily maintain my personal web sites with a Mac or Linux laptop…”
Cedric, I just got notified of your post and what a coincidence!
I am at this very moment enhancing an ASP.NET MVC 3 app that I wrote a couple of months ago. I am coding on my MacBook Pro (running OS X 10.7 Lion). I also worked on it a bit earlier from my Linux Mint 11 laptop. The production application is hosted on CentOS 5 (Red Hat Enterprise Linux essentially).
http://mono-project.com
http://monodevelop.com
I do most of my web work in .NET/Mono and the majority of it is done on Mac and Linux machines (because that is what I have).
My next project is a small app for iPhone and Android. That one HAS to be written on a Mac to target the iPhone. I will be using .NET (Mono) for that as well.
http://xamarin.com/
It is not like .NET/Mono is best for everything. That said, your comment really made me chuckle.
On an unrelated note…If you are a fan of PHP, you might be interested in the Laravel project. It looks pretty nice.
http://laravel.com/release/1.5
#3 by Dan Sutton on August 11, 2011 - 8:24 am
Quote
“PHP is exactly like C. Either you like both or you don’t like either, there is no claim you can make about PHP that can’t be made about C as well, and vice versa.”
– Really? Have you tried writing an operating system or a device driver in PHP recently? I have to say that this comment is one of the more ridiculous things I’ve ever seen.
It’s also probably worth pointing out that C is a time-honored and robust standard, whereas PHP is a buggy heap of barely-coherent crap… but I digress.
#4 by austin on August 11, 2011 - 8:29 am
Quote
one of the best things about php, imo, is that its so much like c.
when i first learned php i had no internet and was teaching myself, i already knew c so i decided to try various functions i was familiar with (like the scanf family or printf family or read and write) and was amazed to see they worked. i could start doing things in php without even knowing any of the vocabulary except what it shared with c.
every day for an hour a day id go to the library,look things up, write them down, and come home and work on php. one of the big reasons i love php.
#5 by Fred Thekat on August 11, 2011 - 11:31 am
Quote
Really? Then you’ll really writing DOS batch files! And DOS batch files that I wrote in 1986 still run on my Windows 7 machine. PHP is no better than VBScript for building web applications. But I suppose that if you rarely use it, and only for web pages, and aren’t familiar with the syntax, and its documentation is great (yes, all the other languages like C#, VB, Java are so poorly documented), then I guess PHP is the right language for you.
#6 by Craig Tataryn on August 11, 2011 - 12:09 pm
Quote
I found this was a great blog post:
http://www.odi.ch/prog/design/php/guide.php
I sum it up as: It’s like any language, you don’t have to write code like you just had a lobotomy. It’s just that people can, so they do.
#7 by David Fisher on August 11, 2011 - 12:25 pm
Quote
An odd curly brace language, looks messy, now working beyond its original role… What? You don’t mean Javascript? But the low level of entry to Javascript rock stardom and the fact it even runs server side now? Seriously? But you can’t build a web UI without having to do it these days.. Php? Oh right. Well Php rocks because I can build what I want in it and it does it well. *I also like JavaScript.
#8 by Dave Lock on August 11, 2011 - 5:14 pm
Quote
Why does every blog article on the net about the pros or cons of a programming language seem to end up in a pi$$ing contest amongst programmers who start their comments with “I’ve been programming for 30 years…”? If your opinion is different, write about the details on your own blog.
@Cédric, nice article, I enjoyed reading it. To me, PHP feels comfortable, scales ok & does its job, & sometimes that’s all a ‘programmer’ can wish for.
#9 by Davyd McColl on August 11, 2011 - 11:21 pm
Quote
Some good points — and very much the reasons why (amongst the 10 or so other languages and platforms I use), PHP has a special place. Yes, you can’t write a device driver in PHP — that wasn’t the point of the article. “Exactly like C” should perhaps have been written “will feel familiar to a C programmer”.
PHP may not have every bell and whistle that a dedicated “everything is an object” OO programmer would like — but it does have the basics which allow you to write cleaner, more extensible code with classes.
PHP exhibits some of the great attributes of another worthy language (Python): it has a relatively low barrier-to-entry but doesn’t run out of steam once you’ve achieved a modicum of ability in it. PHP, like classic ASP, teaches the server-side web developer to respect that the web is not the desktop — unlike asp.net. Not that asp.net is bad — but, like VB did, it allows the developer to create a crappy experience by making it too simple to do so.
But to all of these and all haters, I would say “A tool for a task”. Every programming language and library stack which has survived for more than a year or two must have some merit — or people wouldn’t be using it. If you can find a language/framework/environment which works for your project requirements and isn’t totally blown away by a competing language/framework/environment, then use it! Just bear in mind the usual: functionality, performance, scalability (within the scope of your project) maintainability and ease-of-use.
#10 by deovince on August 12, 2011 - 1:25 am
Quote
Fanaticism is certainly everywhere! a powerful caterpillar is probably not capable of winning a Formula 1 race. But it still very efficient for what it is done. Nice post Cédric !
#11 by CariD on August 12, 2011 - 4:29 am
Quote
PHP is Hypertext Processor for scripting a language in the web language to produce webpages. Though today the HTML code offers better and quick results, the application of PHP becomes complementary
Pingback: StartupDigest
#12 by jlafay on August 12, 2011 - 10:48 am
Quote
I like C and I like PHP, but PHP != C.
Pingback: Knowledge casts « Otaku, Cedric's blog
#13 by CariD on August 12, 2011 - 9:06 pm
Quote
PHP has served its purpose. Still it is competent. Even though new innovations may find an alternative the roots do strengthen the exposure.
Pingback: I like PHP « Otaku, Cedric's blog | Linux Blog
Pingback: Top 10 Wrong Ideas About PHP That You Should Get Right | Dacoders Pvt. Ltd.
Pingback: Top 10 Wrong Concepts About PHP « think foss