I'm a PHP developer. It's what I do for a living. I enjoy being a PHP developer because I generally enjoy coding in PHP. I've written a few posts here in defense of PHP. As such, I'd have to be living under a rock to not have noticed that many other people don't seem to share my enthusiasm. Programming sites all around are littered with "PHP sucks" headlines and articles. In fact, I think PHP might currently be the trendiest language to bash. It used to be Java, and I'm not sure what it was before that (maybe Perl?), but now if you want to find your way into the in-crowd of cool programmers, the first step in initiation is outlining all the reasons why you've always hated PHP.
I read these articles on occasion, and usually walk away shaking my head with disappointment. There are a number of valid criticisms of PHP, because it definitely has flaws and drawbacks (every language does). In fact, as someone who programs in PHP every day, I would wager that I understand many of the drawbacks better than many of the people authoring all the "PHP sucks" articles. But I work around them and continue to love the language. But what really "grinds my gears" is how the criticisms of PHP have really diminished in quality as they have become more ubiquitous. It's now a lot harder to find any "PHP sucks" articles that can succinctly state genuine problems and drawbacks without mixing in false information, matters of personal preference, and things that really aren't drawbacks at all. In any anti-PHP thread, you now seem to find a flurry of young programmers so eager to chime in and add their two cents to the mix that a lot of the arguments that show up lack any meaningful content or criticism.
For instance, on a recent thread I saw about the drawbacks and design failures of PHP, one commenter used one example he thought would highlight the obvious flaws of the language, namely, the "strpos" function. The "strpos" function is simple: it's used for locating the first position of a substring within a string. In PHP, this function returns either a non-negative numerical value specifying the index of this substring, or boolean FALSE. This commenter thought it was ridiculous that a function such as this could return 0 on success, presumably because 0 typecasts to FALSE in an == comparison. However, he failed to realize that just about every other language you can find similarly can return 0 on success (which would likewise typecast to false in any of those languages as well). That was probably the most glaring example of a poor attempt at criticism I found, seeing as he criticized a standard, cross-language function behavior. I pointed this out to him, which got him on the defensive, and then started claiming that it was absurd that it didn't return -1 on failure instead of boolean FALSE. That just left me thinking: why does that matter, really? (And it's still not a behavior unique to PHP by any stretch.) If you're worried about 0 being a "true" value, that means you're evaluating it in a boolean manner: checking if the substring is there or not. So why does it matter to have to check for "=== false" instead of "< 0"? That really becomes a matter of personal preference, not a flaw in language design. The argument reduces to "well I just don't like it," which isn't an argument at all. I can rattle off a long list of languages that similarly don't return -1 on failure for substring searching functions: C, C++, Ruby, Common Lisp, Haskell, etc. Again, this particular argument is just one rather ridiculous claim against PHP, and may be an "outlier" so to speak, but it's one that got under my skin.
The particular article I found the above example was actually a question someone posted earnestly that asked, essentially, "why do you guys hate PHP so much?" So it got a lot of responses, at least half of which I would say are complete junk. But after sifting through them all, I found a couple that I did really like and felt like sharing. First, I offer you a more critical but still very honest quote about PHP:
I also write PHP daily. My conclusion is that it's a fine language for getting things done. There is nothing beautiful about it. If given a choice I'd pick Python every time.
People always say that PHP is so successful purely because it's easy to get started with. I think it's also successful because it scales from small inline HTML pages to large applications similarly easily. Many programmers don't leave PHP because they never run into a problem that it can't handle. It's not like BASIC where you run into that wall quite early on.
PHP is the best bad language there is.
While I can't personally say I'd go and pick another language every day, seeing as I still enjoy working PHP, he makes a lot of good points. I will admit that it isn't a particularly elegant or ground-shattering language. There's nothing you can do in PHP that you can't do in other languages, for the most part. But likewise, the reverse of that is also true, for the most part. It's a high level language, and you can get things done in it. And the real advantage is that you can get things done in it quickly. Another advantage is that it's still extremely popular at the moment, so chances are if you buy some web space, it supports PHP fully. The popularity is also very likely the reason it gets so much flack. But back on the topic of getting things done quickly, here's another quote someone had to offer:
PHP is nothing more than a very thin layer over the C, posix and glibc libraries. If you come from a Linux/Unix C background, it takes about 15 minutes to pick-up PHP.
That guy is spot-on, and honestly that's probably one of the reasons I loved PHP so much when I first learned about it. I had only ever touched C++ prior to that, so it was a very natural and easy transition. But another reason it's still great is that it's a very fast language to both program and run. Combine it with something like eAccelerator and you've got yourself a very powerful and speedy engine to run web scripts. Now I've also seen a lot of people (primarily Python fanboys) complain about a few points: 1) PHP is only single-threaded, 2) PHP has terrible consistency with function/method argument order, 3) PHP has standard library bloat, and 4) PHP lets you get away with too much shit. Being single-threaded is not a problem. It just isn't. The purpose of PHP is to serve up webpages, and you simply do not need the scripting language to be multi-threaded for that purpose, when the webserver already is. PHP is not meant for desktop applications. If you're trying to do something that absolutely needs to be multi-threaded with PHP, chances are you're doing it wrong. (And as a quick aside, if you really want to have threads, you can always emulate threading by calling fsockopen to your own server to open another script, which the webserver will give its own thread to).
Two is a perfectly valid criticism. The lack of consistency is terrible. But to cope with this, most developers I know either memorize the order for the functions they use, or use a text editor that has code hints so they don't have to. And honestly, code hints are freaking awesome, and they make that argument moot. Three I've never really seen as a fundamental problem, but instead more of a "personal preference" issue. Yes, there are a lot of frickin' functions in PHP. I actually see that as a positive thing. That means that if I want to do something, chances are someone else has already wanted to do that same thing and so there's a library function for it. Awesome. I can understand that some people prefer a more minimalist approach, and only want to include things they absolutely need, but again that's really a matter of style than anything else. I have yet to see any PHP application that blows up because the standard library is too big. With op code caching and the like, it's still really fast.
And the final argument I mentioned is simultaneously a blessing and a curse. Honestly, it's the reason I love the language so much--being able to get away with a lot and not having the system freak out at you. But I acknowledge that it can also be the bane of ones existence. I like that things are loose in PHP. It means I can write code quickly and not have to worry so much about all the pedantic details. That's also the reason I loathe Java. Any little mistake, no matter how tiny, I find out about right away in Java. In PHP, I can be more lazy and still get away with it. But I do know well that this also presents a problem: if people can be lazy, they will be lazy. And because of this, there is a lot of awful, awful PHP code floating around out there from programmers who didn't know what they were doing, didn't have time to learn, and were as lazy and unorganized as could be. And if you've ever had to deal with any of their code, you know it's a nightmare. This is probably the biggest thing that turns people off to PHP: debugging poorly written code. If everybody wrote good, elegant, organized code, I really don't think there would be nearly as much criticism of PHP. This is also a reason some developers prefer the stricter languages so much, because you just can't get away with that. But I still like the laid-back environment PHP offers, and as a developer working for a very small company where we write all our own code, it's relatively easy to maintain quality control, at least when compared to larger companies.
But anyways, people are going to continue bashing PHP so long as it remains popular, and it's going to remain popular for awhile. A lot of the alternatives seem like great languages with a lot to offer, but none of them cater to web programming quite as completely as PHP does. Functional programming languages will probably never be popular, because functional programming is hard. Or at least harder than procedural programming. Languages like Python and Ruby have lots to offer, but since they're threaded, that means strings are immutable. And any way you look at it, immutable strings just aren't very conducive to web programming. But then I could be wrong. With the proper framework you can always overcome those limitations to some extent, and Python certainly has been gaining a lot of popularity in programming circles, so we'll see. Though I would recommend this very brief article comparing PHP and Pythong in terms of usefulness for the web; it's good. But for now, I'm still quite happy with PHP, and similarly happy about all the shiny new things that came with version 5.3!
I'm a big fan of PHP. I've been using it for years and think that it is a very powerful language. Although I do not use it to the level that you do, I've found that it does everything that I need it to do. I usually work in a LAMP environment. The price is right and when the technology fails, it's usually my fault.
You have an excellent post on PHP. Good work!