Stop Hating On PHP And Learn To Code Better

So here we have the timely battle that PHP faces. Frankly, I’m getting sick and tired of this. All in all, I’d have to agree with Jeff on this one, to a certain extent.

They Say PHP Sucks

You’ll hear the same claims every single time: “Google PHP Sucks you’ll see how much PHP sucks”. Oh please! This is some of what the haters usually use for the argument:

  • There are too many functions
  • PHP Isn’t OOP
  • Everything is strung together – This claim usually talks about the interaction between PHP and/or SQL

In Defense of PHP

The number one claim is that everything in PHP feels like it’s clunky and may fall apart at any time. Granted, I’ll be the first to admit that I hate boring SQL queries. Keyword here is boring. Within the past year, I’ve really gotten to see that SQL has a lot of power. Learning of all of awesome power, just makes me hate writing crap like:

select title, body, post_date from posts where id = 32

Functions, Functions, Functions

I, myself, have never really understood the “too many functions” argument. Maybe it does make the documentation a bit muggy to get through, but how many functions that are in there have very little to do with me. I don’t care, I just use a handful of them.

PHP is OPP – That’s All There Is To It

We need to make a distinction here: PHP 5 is Object Oriented. The problem is a lot of PHP code that you can find out there isn’t. PHP has this classic gift and a curse: it makes it damn easy to get something functional out the door in a hurry.

I mean, I’d love to see a bit more OOP, but that’s an implementation issue. And the piles of arrays for data storage are a lil’ bit of a pain, but again, that’s an implementation issue.

PHP Is Not A Framework – Get Over It

Now, I know what you’re thinking: “Baz, you’re a lazy bastard, that’s isn’t so hard to write”. And you’re be totally right. But after the 25th time writing this or some simple variation of this, you can’t help but feel that you’re wasting your time. Now here’s the kicker: This isn’t a problem isolated to PHP. Most other programming languages (listen to me well: Programming Languages) suffer the same fate. You’re going to see SQL strung out throughout code in any language, I don’t see why people keep blaming PHP for this.

This is a classic in the PHP vs. Rails saga. Haters often compare PHP, a simple programming (scripting even) language to a very complex web development framework. I don’t care what you say, but that’s like comparing a factory stock model car to a car with 2 years of work and $20,000 of after market parts. Luckily, the folks at Rails Envy have made an attempt at clearing this up with their mention of CakePHP:

Time To Take Responsibility

As programmers and web developers, I think it’s time to take a lil’ bit of responsibility for the type of code we write. I don’t know about you, but regardless of the language, I can write some very nasty code. It all depends on the frame of mind. I’ve seen varying levels of PHP code: I’ve seen dozens and dozens of code blocks (not functions) in one index.php file. I’ve also seen nice, clean, refactored, DRY code.

Granted, PHP makes it a bit easier to be lazy, but do we blame the language for that? Come on guys, let’s put on our big boy pants, read a book about code design, and apply it to any language that you’re coding in. PHP powers some of the largest sites on the internet, someone’s doing it right. Stop being mad because you can’t. Stop using the programming language as a crutch and learn how to apply proper coding techniques to any language.

Bottom Line:

Some of the largest sites on the internet — sites you probably interact with on a daily basis — are written in PHP. If PHP sucks so profoundly, why is it powering so much of the internet?

The only conclusion I can draw is that building a compelling application is far more important than choice of language. While PHP wouldn’t be my choice, and if pressed, I might argue that it should never be the choice for any rational human being sitting in front of a computer, I can’t argue with the results.

Source: PHP Sucks, But It Doesn’t Matter [Coding Horror]


  1. Luis Larrea says:

    I wasn’t aware ppl were complaining about PHP. I’m not sure what’s driving this but you’ll find complaints to any OOP language. Look up Actionscript 3. There’s hardcore moaners complaining about it since there’s such a sharp contrast in scope between it and it’s predecessor.

  2. The sizes of sites that PHP powers don’t mean a lot; MySpace was originally written in ColdFusion, of all things.

    The function library is not problematic because of size, but because of organization. When you have THOUSANDS of builtin functions and neither a real package system nor namespaces, organization is kinda important. But instead of builtins being object-oriented — which would then make the LANGUAGE, in actual practical use, also be so — there’s just a myriad of functions that each take some object or handle as a first parameter. Of course most PHP code isn’t going to be OO: nothing in PHP’s core is! Worse, they’re inconsistently named at best, a lot are duplicated with very similar functionality (there are some 23 functions for finding one string in another), there are messes like mysql_real_escape_string (instead of mysql_fake_escape_string!), etc.

    Presumably the SQL complaints are about PHP’s lack of an ORM library with a lot of mindshare? Ruby has ActiveRecord, Perl has DBIx::Class, Python has SQLAlchemy. I’ve never seen one used with PHP at all.

    Most of PHP is a patched-together mess, yes, and I don’t know of a single advantage it has over the other open-source scripting languages.

  3. @Luis Larrea: Yep, there are always going to be complainers. I say, if something else works for you, use it. Shoot, I’m currently looking into Ruby on Rails just to see what it has to offer me. Every language/framework/tool/etc is going to have it’s good and bad.

    But you cannot deny that PHP just works!

  4. @Eevee: But you are illustrating my point. If PHP had problems, which made it fundamentally unusable, then why are there so many large sites that use it? There has to be a reason, and that is my point. PHP just works!I am not arguing that it’s the best tool for every web job, I’m not saying it isn’t disorganized, I’m not saying the PHP code is OO, because frankly I don’t care. I don’t care if there are “THOUSANDS of built in functions” as long as the 20-30 I need work like they are supposed to.Now on to your point of ORM libraries. This is the classic argument that people give about PHP. And to this I say: “come up with something else”. I’m sure about the others, but “Ruby” does not have an ORM library; ActiveRecord is a part of Rails, which is a huge framework by itself. PHP actually does have such libraries: (List of object-relational mapping software) [Never heard of or used any of them, though]. But you are falling into the same boat as everyone else who compares PHP to Ruby On Rails.PHP has it’s issues, but it has proven itself a good work horse at cranking out web applications.

  5. It doesn’t have problems that make it fundamentally unusable; just problems that make it not as good a solution as other tools. A lot of large sites presumably use it because it is popular, and it is popular because embedding extremely lax source code within HTML let a lot of nonprogrammers get their foot in the door. Popular things do not become popular necessarily because they are good; they become popular because they are good *at getting popular*.

    I’ve never heard ORM used as an argument about PHP before I read this post, and I’m not using it as one myself. I’m just pointing out that ORM use seems to be unusually uncommon within the PHP world, which may explain the complaints *you* have heard. Personally, I would blame this on what I perceive to be a disproportionately large number of amateurs within the PHP community.

    It has PLENTY of issues, and there is nothing about it that makes it significantly better than other languages. I can name plenty of issues I have with Perl or Python, too, but they are mostly design disagreements or backwards compatibility. PHP’s are more fundamental problems with consistency, unexpected side effects (often in the name of being “easy”), disorganization, missing/broken features, and all manner of other such roadblocks that make PHP take far more effort to work with than should ever be necessary.

    Lack of closures alone is almost a deal-breaker; they are an odd feature but have left me with much simpler and cleaner code on numerous occasions. Functions aren’t first-class objects at all, so there are no function pointers. The cavalier attitude with errors or warnings (essentially “never ever break no matter how absurd the alternative”) makes it harder to find problems and doesn’t yield results much better than an error page. The function library is useless unless I look through the thousand-function manual every time I want to do something — and make sure I pick the right one out of two dozen alternatives. Substitution with eval is completely broken. Database access is STILL a mess; SQL injection is rife in PHP code as the manual still doesn’t even advise using prepared statements, so a lot of wild code is full of mysql_real_escape_string and magic_quotes and whatever other manner of layers of hacks to work around a problem that was solved a decade ago in every other language. The object system is a joke; there are no friends, static methods don’t know what class they’re being called on, constructors can’t be variadic, equality can’t be overloaded.. There’s no module handling or versioning. There’s nothing to hold a torch to CPAN; there’s PEAR, but it seems oft-neglected, so the common case is to just use the myriad of builtin functions and roll your own if you need anything else. Since everything is built in, if you need a module your host hasn’t compiled support for, you’re screwed — whereas installing any version of any module locally in Perl or Python or Ruby is easy. Unicode is a joke. Arrays and hashes are intermingled, giving a very ambiguous sense of when I’m using which (what if I want a hash with numeric keys? I’ve done this half a dozen times, but needed an ordered hash only once in my life) and leading to yet more cognitive overhead; array functions also become far more complicated than they should be, as they have to account for any sort of combination of the two. References aren’t really references, and there are no weak references, which makes avoiding circular references a lot more convoluted in some cases. Embedding code within HTML is, again, a terrible idea. Strings and numbers are the same sort of variable, but there is neither real typing nor different operators for comparing each, so there’s more for me to worry about even when comparing values. Array access syntax is broken; I can’t write something_that_returns_an_array()[$i]. Reflection is a pain and inconsistent with the rest of the language anyway (suddenly OO?), but the lack of first-class functions doesn’t help. The sigils ($) don’t even have any reason to exist, unlike in Perl where they denote variable dimensions, but they permeate the language anyway.

    High-level languages are supposed to be a tool, to do work for me that I shouldn’t have to worry about. PHP might as well be the assembly language of Web programming: a large mass of functions but little functionality outside of that. Little things like comparing numbers to strings might seem trivial, but when you have some quirk or side effect to worry about on every other line, it becomes a monumental distraction from just writing code and makes avoiding bugs that much harder.

    The other languages are richly-featured and well-designed. Even Perl 5, suffering from more or less backwards-compatibility with Perl 4, manages to remain powerful, flexible, and mostly consistent. They all have built-in and simple reflection, functional programming constructs for when they make sense, fully-featured CGI libraries and rich sets of other third-party libraries (all in their own central packaging systems and loaded by real module support), rich OO support, and very simple standard libraries that do a small set of operations well.

    I have never heard a compelling reason to use PHP for anything more complicated than a page counter. Even the Web-targeted features in PHP are ones that tend to be avoided in larger projects, where more organization is needed and frameworks should be taking over. It works, sure, but that is a trivial milestone to pass, and there is nothing about it that makes it work better than another scripting language.

  6. OFL!
    I can’t help but smile. You made a lot of very good points. And please understand, by not means am I a PHP evangelist. In this very post, I’ve mentioned my interest in Ruby on Rails.

    But you seem to be missing the point: For what it does (which is getting a website out of the door with a minimum of fuss and muss) PHP is pretty well suited for it’s task.

    A problem that a LOT of developers have is not realizing when their projects have reached the limits of PHP (at least with no framework attached). They keep on plugging away and then complain about PHP’s limitations.

    @Eevee: Clearly, from your vast list of problems, you have come to a point where PHP no longer suits what you’re trying to do. And that’s fine, but ot say that it isn’t suited for “anything more complicated than a page counter” is exactly the type of whining and complaining I’m talking about.

    Man used to hunt with a rock. Just because we now have guns, doesn’t mean a rock still can’t bash someone’s head in.

  7. I have a question.

    What part of “pick the tool that fits the job” do some people tend not to understand? 🙂

    And it’s not all about the technology itself. You need to take into issues such as knowledge gaps, skills gaps, time, money, extensibility, and more …


  1. […] Old presents Stop Hating On PHP And Learn To Code Better posted at Web Development 2.0: Web Design, CakePHP, Web […]