Skip navigation.

Why I Like PHP revisited

A quick response to all the "PHP sucks" postings listed in PHP Developer that were sparked by Tim Bray's original PHP rant.

Yawn... been there, said it before.

But given that I like a good controversy, and our memories are short, here's my view again:

  • When I first coded in PHP, I treated PHP as a better Javascript that I could use on the server-side. True Javascript isn't perfect either but we all use it for designing user interfaces and web apps. And if we can accept Javascript (an admitted hack by its designer) as a language for enterprise use, then it seemed obvious to me that there was no reason why PHP isn't suitable for the Enterprise: PHP has a familiar C-style syntax, it scales, PHP has builtin support for the template pattern (the very heart of MVC if you like it), you can write modular code, use inheritance, and code in teams.

    It's just some people don't like it and snipe at the edges.

  • PHP is, and not by some twist of fate, good for developing web apps. Allowing HTML and PHP to intermingle in a .php or .inc template is pretty good. It maps to a very natural way of working. So the success of ASP, JSP and PHP are not really by accident. In contrast, you can write pretty awful Python or Java code to output HTML using print or java.io if you don't use a template pattern, which is built into PHP!

    There have been abuses of the template language, notably PHP Nuke (and not only by others, i've been guilty too), driving serious programmers to scoff at PHP. I think they need to do more and read less.

    Take my example: when I first started trying out PHP, I didn't worry about all the code abuse I saw out there. I've coded in Perl, and seen the same shit before. What I did to try PHP was write some web pages to see how easy coding was, wrote some OOP classes (the start of ADOdb), then benchmarked the results. Based on my checklist, i knew it was good enough, and that was in early 2000:

    1. Support for classes and modular code: yes.
    2. Easy to code: yes.
    3. Easy to teach and plentiful docs: yes.
    4. Templates: yes (the first release of Smarty was the clincher).
    5. Speed: faster than ASP based on my benchmarks.
    6. Scalable: near linear scalability based on my apache-bench tests.
    7. Customizable: err, its open source...
    8. Platform agnostic, no anti-Microsoft idealogy: yes, support for Windows not an after-thought.
    9. Large PHP community and high potential for growth: yes and yes.

  • PHP also has a great library of functions for string manipulation, database access, graphics manipulation, and for accessing weird but useful external software such as memcached. Does PHP have library inconsistencies? Yes, probably inevitable given the enormous size of the standard library of extensions and the open source nature of its development. Are the inconsistencies important? Probably not, unless you don't want to use PHP.

  • PHP breaks down the ivory towers (thank god!) Unlike Java or Python, you can have a team with different skills using PHP. The hardcore programmers build the core libraries, and the web designers and more sociable programmers working on the user interfaces, all coding in PHP.

  • PHP is mainly a string processing language. That makes it really good for manipulating HTML. Features like "0" == "" and weak-typing work well for text processing(see note 1). Some people have said that PHP's Unicode support is not fantastic. Agreed, there is not enough native Unicode support outside UTF-8; but despite that PHP has been a fantastic success even in countries using double-byte languages. That cannot be by accident or due to ignorance.

  • The only real PHP gotcha is that PHP cannot do everything. PHP is good for web app user interfaces and business rules, and as a shell scripting replacement. If you look at big PHP apps, the stable core is written in another language, C/C++ (Yahoo), or Java (Ning), or PL/SQL (my company). In a sense, all the interesting techie stuff in PHP is happening at the C level, in the PHP extensions: in pdo, gd, oci8, mysqli, etc. Everything else, PEAR, Smarty, Drupal, Sympony, ADOdb, PHPLib etc, is just fluff :)


    PS: But if you're really good, you can build 90% of your enterprise app in PHP. See Flickr's architecture (pdf).

  • If you are looking for a better Java or its replacement, sorry it hasn't been invented yet. PHP doesn't come close. I do think that Jython comes close, but it lacks the Java-style syntax required to make it really popular, and there's no one (that I know) speeding up Jython the way Jim Huginin is tuning IronPython for .NET.

Note 1: You use strlen($str) == 0 to check for empty strings - that's a fundamental string-matching pattern in PHP.

Also see Harry Fuecks pro-PHP rant.