rc3.org

Strong opinions, weakly held

PHP is bad

Tim Bray says of PHP:

If you want your ears bent back, have a listen to Zend CEO Doron Gerstel; he’ll tell you that half the websites in the world are powered by PHP and that there are 2½ million developers and that the war is over and PHP won. So here’s my problem, based on my limited experience with PHP (deploying a couple of free apps to do this and that, and debugging a site for a non-technical friend here and there): all the PHP code I’ve seen in that experience has been messy, unmaintainable crap.

I can go one further and say that all the PHP code I’ve written has been messy, unmaintainable crap. It pains me to say that because it’s code that I wrote, and I know how to write code that is maintainable and neat. I do it in Java and Ruby all the time. So why can’t I do the same in PHP? I don’t know, but it seems to be impossible. I rewrote somebody else’s reporting system in PHP last year, and it turned out to be garbage. So then I rewrote that system again, and it was still garbage when everything was said and done.

I do think that one big problem with PHP is that refactoring is extremely difficult. When I have some messy code in a Java system, I refactor it. I find refactoring a lot more difficult to do in the PHP world, and the lack of a tool like Eclipse that facilitates refactoring is a big part of that. I’m not just talking about the stuff under the Refactoring menu, either, I’m also talking about the fact that Eclipse understands your code well enough to tell you about your compilation errors before you actually compile anything. That makes it a lot easier to move things around without having to round trip things to the browser to make sure they still work. And then there’s the fact that Eclipse has a JUnit test runner built in so that once you’ve done your refactoring you can easily run regression tests to make sure you haven’t broken anything.

It will be interesting to see how easy it is to refactor Ruby on Rails applications. The editors are not as powerful as Eclipse is for Java, but Rails is built to be easily testable, and if you take advantage of that you’re pretty well insured for refactoring.

35 Comments

  1. I had as much as I could take of the code-in-html paradigm doing ASP development from ’97-’99 (and one more unfortunate project in late 2000/early ’01, done in ASP despite objections). As a result of those experiences I’ve completely avoided PHP… never learned it, never used it, never plan to. Every time I’ve looked at a PHP-based system I’ve cringed and tossed it aside. Messy, unmaintainable crap, as you put it, is right on the money.

    I briefly tried a PHP-based system, WordPress (1.3 or 1.5 I think), and it too was a mess. I tried to customize it and found I just couldn’t do what I wanted because the files had so many circular dependencies. It worked the way it worked or not at all. Spaghetti. Every PHP-based CMS I’ve tried has had major functional or extensibility limitations which I attribute to the language and/or the way people tend to use the language.

  2. Well, if you really like using Eclipse there is a Ruby plugin, and even a Developer Works article:

    http://www-128.ibm.com/developerworks/java/library/os-rubyeclipse/index.html

    I have no idea how easy it makes refactoring. Although, why you would use Eclipse over TextMate escapes me 😉

  3. PHP wields a two-edged sword. I find it very easy to be productive with the language; it’s the classic, ‘because you can, doesn’t mean you should’ paradigm.

    I find myself spending loads of time refactoring and just plain re-writing lots of PHP code. The mixing code and presentation model is always the first model most PHP hacks are exposed to. Had a standard model for seperation existed from the beginning, perhaps these bad habits wouldn’t be so prevalent today.

    One of the better PHP projects i’ve seen in a long time is MediaWiki.

  4. This is odd to me — there’s nothing inherently worse about PHP, so I don’t get what makes it more likely to create a bad app. Perhaps it’s more accessible to non-programmers, meaning that there’s a crop of apps out there that aren’t written to traditional programming standards? (This is as opposed to Java and other compiled languages, which are much less likely to attract casual app writers.)

    It’ll be interesting to see what the current trendiness of Ruby on Rails does to Bray’s (and your) perspective on the relative maintainability of apps written in Ruby; I suspect that the number of non-programmers writing Ruby apps will make a difference. In that respect, I’d argue that the language doesn’t have a lot to do with it…

  5. I would buy into the novice programmer argument except that I find it more difficult to write maintainable apps in PHP and I have no trouble writing them in Java. The jury is out on Ruby. There are a lot of novices who program in PHP. That’s a strength of the language. But I think that for a number of reasons it doesn’t lend itself to writing maintainable code. I really wish it did — PHP has a great learning curve and is incredibly easy to deploy.

  6. Maybe PHP is too easy, I don’t know. I’ve written a couple simple things in php: a shopping cart, a class project for algorithms, a website. Most of my horrendous php code was from the beginning of a project when I didn’t really know what I was doing to begin with (in php or in the project I was working on). In the well-defined area, such as my algorithm project, it looks pretty similar to my C/C++/Pike/Perl code.

    That being said, I learned most of my programming from MUDs and college classes. The MUD’s certainly didn’t help my style much, although they ended up helping me learna lot of things. I tend to think the classes eventually helped my style enough to point my code more toward readability.

    At the end of the day, I dislike Perl more than PHP, for pretty much the same reasons as you state, but only when it’s not my own code. I think this has more to do with the fact most people aren’t very good at writing maintable code. You can either take the tact people should stick to a language that enforces some rules for code, or you get people to learn a better coding style.

    I’d always opt for the latter, myself.

  7. For years I’ve bought the “you can write crappy code in any language, PHP just makes it easy, but you can also write elegant PHP argument.”

    But I just don’t think the data is there to support it. I keep looking, but I’ve never seen elegant PHP, ever. And despite my best efforts, and being rather good at PHP, I’ve never written PHP code that did anything more then work.

  8. For all you who say you can’t write elegant, or well-structured, or nicely-refactored PHP code — what is it that prevents this? I’d love to know what the cause is.

  9. Christopher Thompson

    February 20, 2006 at 1:06 am

    What kind of programmer are you that all the PHP code you produce is “messy, unmaintainable crap.” My PHP and Java code looks pretty much the same — and when a client asks for changes to a PHP system they are easy to implement because the code is fairly well designed. So what’s your problem?

    And since you don’t know about the syntax checkers or SimpleTest integration in Eclipse, I have a hard time taking your comments seriously.

  10. Rafe, I think you should change the title of this post to “PHP is bad for professional programmers”.

    I am a geek (I read you, don’t I?) but not a professional programmer in any sense of the definition.

    I write code when I have to but that’s it.

    I use PHP because it’s easy for me and I get things done. I am using the ZEND editor which prevents me from making stupid errors and helps me write code faster.

    Your world and mine are totally different worlds.

  11. Why is PHP worse than other languages? I’m currently maintaining a rather complex PHP-based site, written by a colleague of mine who’s a very competent programmer. The code is crap. Despite being designed from the ground up to take advantage of OO style programming, the deficiencies of the language prevent either of us from writing agile, modular code. Check out my (very sarcastically written) post on PHP for a short list of the deficiencies that I’ve run into:

    http://skizzy.biz/2006/01/28/php

    If someone wants to say “there’s nothing inherently wrong with PHP,” I would challenge them to read that post and address the points in it.

  12. Pat Berry says “one of the better projects I’ve seen in a long time is MediaWiki.”

    But internally, MediaWiki is unmaintainable spagetti code! Try to do ANYTHING in MediaWiki and you’ll find yourself lost in a mass of code and presentation.

  13. I’d be interesting to hear why developers are finding it easier to write elegant code in Java but not in PHP.

    Has anyone taken a look at SugarCRM?

  14. Then you haven’t looked a Drupal (http://drupal.org) yet. It’s got one of the most well factored designs I’ve seen.

  15. Why is it hard to write good PHP code? Lack of a good module system, among other things. It has a lot to do with how people learn the language and what idioms are popular. Perl5 has exceptions, sorta; but the standard library doesn’t raise them, so they don’t become a habit. I wrote a little bit about this recently: python, javascript, and PHP, oh my!

  16. I’ve actually written clean and reasonably elegant PHP code, and hated every minute of the process. Everything from PHP’s database APIs to its CGI parameter handling is subtly hosed. As soon as you want to do anything correctly–be it security or Unicode–the system will frustrate you at every step.

    It’s really, really easy to write cruddy PHP code (and that’s fine for novice programmers working on small sites), but writing professional quality PHP code is a long, ugly slog, with the system fighting you at every step.

    Years ago, I hacked a bit on the PHP interpreter itself, which was also an ugly piece of spaghetti code, infested with security holes. There were format string vulnerabilities, missing arguments, and a half-dozen other kinds of lossage in the C source.

  17. It is quite right that a lot of PHP code out there is messy. “Not-messy” mostly means object-oriented, and with PHP4 is has – let’s face it – not really been possible to do object-oriented development.

    PHP (and scripting, in general), is deceptive in a way. You can create a working solution (or, at least, some visible output that even looks good) really quickly. But if it comes to maintaining the code, life gets tough.

    Still I have found that refactoring PHP works extremely well, if you do it right. To a certain extent, it even works without automated tests.

    While refactoring may not always the best solution to maintain old and messy code, it sure is a great way of educating people to write better code in the future.

  18. IMHO it just takes some discipline. And a few self-written (or copied) libraries. I don’t see why you hate php for having a few less logic bits. It never claimed to be more than a scripting aid for webpages and does an excellent job at that.

  19. I got here from Harry Fuecks article on sitepoint. I’ve been reading all the links from his post and the links from those posts etc for the last 3 days.

    I’m a web developer. I went to school and learned how to code … I think (Java, C, C++, Perl), but when I started doing actual coding it was with PHP. I haven’t been coding professionally for long (just 2 years), but at the risk of sounding like an idiot. I think there is a key thing that you guys are all missing.

    PHP wasn’t designed to be pretty.

    I know it sounds like a cop-out,but just sit on it and let it marinate for a bit. Every single PHP book I’ve ever read has a short blurb about PHP’s history and I think if you read its orgins, you will see where it gets its characteristics from. At its very essence (to me anyway) PHP started out helping you spit out conditional HTML (HTML++). So

    if(they haven’t clicked) {display this image} else {display that image}

    But the web has evolved, and because of the demands of the community and critics (like you guys :]) PHP is evolving too. Remember that PHP did not start out as an application language. So I think its unfair for it to be compared with languages like Java, C++ or C. Those are application languages and they were written for that purpose. I mean, just look at the trouble Java has had adapting to the web. Isn’t it telling that you can’t/shouldn’t even code a small website with Java?

    I keep hearing all this stuff about “elegant code” (heard it all through college too … weirdly) and I am aspiring to it, but what you “badass” programmer types need to understand is that PHP is popular because it works. It does what you want it to do. If that essential nature of PHP can be harnessed and then refined to allow for more structured code design … then the future is bright … no?

    PHP is PHP is PHP …. Its unique (now that ASP is dead and gone) and it is the king of the web (for better or worse).

    PS:Leave PHP noobies alone. They are the reason PHP is what it is today. C++, C and and Java are institutionalized … PHP is a grassroots movement :]

  20. I love how people compare PHP to ROR, Java, .NET… It’s not..

    PHP is a platform for you to build on, unlike .NET/ROR (i dont know Java) its not built with its own framework. If you don’t like its DB API’s – re-write your own, or find an Open one you do like — that’s the whole point..

    You cannot be forced to write crappy code – come on.

    You wrote a shopping cart? thats like a 2 second job, and doesnt touch on how powerful PHP can be when used correctly.

  21. PHP lies somewhere in the middle ground between “framework” and “language”. Obviously there’s a language in there as well, but there’s also functionality to handle request parameters, deal with sessions, add headers to a request, and so forth.

  22. Steinar Elissen

    March 24, 2006 at 4:35 pm

    I could not agree more. I find it disterbing that PHP has such a huge share of development. It’s “PERSONAL home pages”, with personal in capital letters. Am looking over old PHP-code that I wrote, I try to rewrite it, it’s just messy in all aspects. I must admit, ASP is worse, but there is a reason M$ has left ASP to die and gone for ASP.NET.

    JSP using Struts / JSTL/EL is just a DREAM compared to anything. It takes a bit of getting used to, and learn though. So PHP is good for personal usage and small, simple sites. But.. It’s used for way too much…

  23. Check the elegant and clean ‘Code Igniter’ framework for PHP…

  24. You Guys Suck

    April 6, 2006 at 3:02 pm

    For such unmaintable crap I sure do successfully maintain a lot it without any problems whatsoever.

  25. It’s amusing how someone plays around with a few kiddie scripts and procedural compilations and claim to know the ins and outs of a language.

    I mean seriously, these scripts that you had played around with are of a lesser standard than what you could churn up in the language of your element.

    People with the necessary tertiary education that had application design and OOA&D ingrained into them for years since day 1 pick up a script which was written by a coder who has spent 2 weeks (without sleep! – yes that counts for somethin’ 🙂 ) and say “hardy hardy har har” this is a hobbyists language.

    I’ll have you know that there is a flip side to the coin, there are developers out there who have developed frameworks (which work and work well) using the horrible object model that php4 has provided.

    That takes skill, patience and a solid grasp of application development and related practices and methodologies.

    PHP does not answer to the criticism of the naysayers, they react to the direction of its developers.

    As developers, we have contributed to the push of the language to achieve what the object model is now in PHP5.

    No sh*t PHP 4 is bad, the biggest thing was the pass by value by default. This has been remedied with the latest version of PHP (that’s 5).

    But still, to play around with some prebuilt scripts and/or write one module and say that it is “messy” and “unmaintainable crap” – most likely due to your inexperiece with the language – is short sighted and somewhat “petty”.

    As for the poster who outlined PHPs short comings in his rant, no one here is saying that there is nothing wrong with PHP, we are merely saying that it is possible to create code that is clean and maintainable.

    And why does one poster find it “disturbing” that all these sites are made with PHP? to your experience (and I doubt that you’ve had any serious development experience with PHP), which part of application development with php did you find messy? and what grounds do you base the conclusion that PHP can only handle small sites? Is it because when you googled for PHP that you found an outdated meaning to the PHP acronym just so you can throw your hat into the fray?

    It started off as Personal Home Pages… It evolved PHP/FI (FI stood for form interpreter) It was taken by Gutmans and Suraski who rewrote it and forwarded it to the creator Rasmus who accepted giving born to PHP3 – this is where the “Personal Home Pages” tag was dropped. PHP4 came – The object model was an afterthought, then tehre were the fixes… the partially broken code…

    I think the biggest mistake that PHP made was dubbing “PHP 5” as “PHP 5” – they should have done a Microsoft and called it “PHP.net”, “PHP.org” or something that did not associate the new version which has partially broken backward compatibility.

    But anyway, that’s outside the scope of this commentary.

    Elegant and clean code is attainable with PHP, you just need the necessary experience of writing maintainable and clean code.

    regards,

    rvdavid

  26. Was it is_set() or isset()? Errr err… Was it var $variable or simple $variable? Was it public static var $variable or … ? Is it self::$variable or self::variable? Is it $this or this? (parent, self) Is it $this->$variable or $this->variable?

    REDESIGN PHP!

  27. “Was it is_set() or isset()? Errr err… Was it var $variable or simple $variable? Was it public static var $variable or … ? Is it self::$variable or self::variable? Is it $this or this? (parent, self) Is it $this->$variable or $this->variable?

    REDESIGN PHP!”

    LoL you need to get a clue Dan… log onto http://www.php.net and do some research. If the syntax confuses you, then you’ve got no hope learning PHP let alone any programming language.

    Try looking at Ruby syntax you’ll have a ball with that one. mwahahahaha get a clue! lol

  28. “I can go one further and say that all the PHP code I’ve written has been messy, unmaintainable crap. It pains me to say that because it’s code that I wrote, and I know how to write code that is maintainable and neat. I do it in Java and Ruby all the time. So why can’t I do the same in PHP?”

    I don’t know. I write clean, maintainable code in PHP all the time. Of course, it helps to use a somewhat MVC design pattern, and a decent framework (I use Fusebox). I really hate these criticisms of PHP development problems as if they were parts of the language.

  29. richard holker

    July 8, 2007 at 10:10 pm

    “Was it is_set() or isset()? Errr err…” It’s isset()

    “Was it var $variable or simple $variable?” var $variable is only valid in PHP4 where visibility operators were not supported. $variable is your normal variable.

    “Was it public static var $variable or … ?” in PHP 5, it’s public static $variable – the use of var is discouraged in php 5.

    “Is it self::$variable or self::variable?” use self::$variable to access static variables. use self::variable to access class constants. Although it should be in upper case.

    “Is it $this or this? (parent, self)” this is not valid syntax, unless you were meaning to access a constant. “parent” is used in all languages as is “self”. Are you a programmer? or just some kind of troll?

    “Is it $this->$variable or $this->variable?” Both are valid. PHP Supports dynamic variable names.

    so you could do something like:

    $variable = ‘foo’; $this->$variable = ‘bar’;

    echo $this->foo; // returns ‘bar’

    As put in a not so eloquent way by “Dan you dork”, you need to read up on your basic syntax before using PHP – let alone any language.

    Hope this helps 😉

    You dork! haha

  30. I do not think that it’s advantageous for PHP that you can get something working quick and dirty.

    The nature of PHP is that it’s easy to write crap code that works, but as a programmer you don’t have to and you shouldn’t ever do it.

    Ultimately, it is your responsiblity as a programmer to write good code, and that is entirely possible with PHP.

    Also it’s a good idea to look into frameworks. Ruby as a framework called Ruby on Rails and Python has one called Django. PHP has many, the most popular is probably CakePHP – http://www.cakephp.com

    Zend is more of a library than a framework.

  31. PHP produces the same results like ASP and JSP, PHP does it a bit faster. The matter of fact is that it really depends on what kind of programming syntax u do like. Me, I like more ‘C’ like code and that what it made me choose PHP. JSP is a bit close but ASP is very different, and more user-friendly. with PHP ( even JSP or ASP ) you can develop really dynamic applications, which can be left not maintained for ages, and no problems!

  32. Have you look at the framework symfony. There is a very good 3 levels separation between model, control and views, a very good documentation.

    You can write good php code with it. When you look at a symfony project you have not written, you are very confortable to understand what is inside.

    See : http://www.symfony-project.org/

  33. ==================================

    ==================================

        I strongly disagree with most people here!!!
    

    ==================================

    ==================================

    Wow… all I can say to all these guys here that don’t like PHP is: you are simply bad programmers and just don’t know how to write well organized code in PHP! The problem is not PHP, the problem is you.

    I have been working for 6 years on a single PHP project, a huge and very complex website, and once I organize my code correctly I have had NO TROUBLE. I just LOVE PHP, because it gives me so much flexibility to do all kinds of things and do it so easily. But it’s true, the programmer has a RESPONSIBILITY to organize code – and unfortunately many programmers don’t do that.

    As someone here said “Ultimately, it is your responsiblity as a programmer to write good code, and that is entirely possible with PHP.”

    I have been working with other languages as well (eg. Actionscript, C#) and just the complexity of doing a simple little thing makes me very hesitant of embracing them.

    I’m not saying PHP is the single and only solution for everything, but based on experience in PRACTICAL and LARGE programming projects I do have my strong love for PHP because it’s so powerful and mature.

  34. To get the first thing out of the way, if you’re programming in java eclipse will be one of the better IDE’s. If you’re going to program in PHP, find and IDE which has been made for PHP development! That will save you tons of trouble to start with.

    Next thing: if your code is messy, try learning the language! PHP is as forgiving as you want it to be. If you want it to be forgiving just don’t do anything. If you want it to be as strict as Java, then check data before working with it. If you want it to be stricter, there is no one to keep you from implementing it.

    If you can’t organise your code, then it is not to blame on PHP. Java will allow you to create a complete mess of your code as well. Try it by starting a new project and putting everything in a single method.

    The point I’m trying to make is that you can make it as bad as you want to, in any given language. It is your job as a developer to not do so. And whether you’re going to work in C, Java or PHP. They will all allow you to create a mess! It’s about how you deal with it.

  35. Would you believe a house builder who says they can’t build a decently constructed house because the insulation isn’t pretty? Your argument against PHP, to me, falls short of providing any real evidence of your claims at all.

    In my experience most developers are extremely lazy despite knowing better, and have very lofty, unrealistic, academia-esque definitions of the word “elegant”. How often do you write code in other languages, then revisit it in 12 months to make changes and stare at it in awe, thinking, “wow, this is really great stuff”?

    I can understand arguments about PHP code looking eye-gougingly sloppy when interspersed with HTML, CSS, etc. It truly is. But the same can be said of ASP, JSP, ASP.NET, etc. That mishmash of code is often an amateurish way of doing things to begin with – this is the kind of mess you see in so many tutorials out there, and unfortunately is how people learn the language. But it is NOT forced on anyone by the language itself.

    I started using PHP in 1997 when version 3 was out, and have seen the language and my skills with it come quite a long way since then. And I can confidently say I’ve been writing clean code in PHP (and a slew of other languages) for many years now, which did what it was supposed to do, did it well, and have never had trouble maintaining it — that is, unless I didn’t put enough time or effort into designing and writing it well to begin with. Perhaps that’s because I write OO code 95% of the time, keep my view-layer code entirely separate from model and controller level code, spend at least an equivalent amount of time designing than I do coding, generally make the code readable by properly spacing things out, don’t name my variables $a and $b, and don’t name my functions doit() or go(). I’m not saying you do any of this specifically, either — but my point is that I’ve developed good, language-independent habits that have allowed myself and others to maintain my code without problems, and have made collaboration with other developers easier. Continuing those good habits in PHP has not been an issue for me in the least. So while I can definitely agree with you that most code seen in the wild is terrible, I profoundly disagree that it is PHP’s fault.

    Blaming the language is a cop-out for laziness. Plain and simple. If you can only write crappy, unmaintainable code in PHP, that’s all on you. Perhaps if you spent less time whining about the language and more time focusing on your own shortcomings and using that knowledge to improve your actual process, you’d be happier with the results.

Leave a Reply

Your email address will not be published.

*

© 2016 rc3.org

Theme by Anders NorenUp ↑