PHP Sucks, But It Doesn't Matter

@Mo - Like I said, I’m sympathetic because of my background in “non-professional” languages like Basic, but PHP is almost pathologically bad! Here’s one example:

http://www.bitstorm.org/edwin/en/php/

Some function names consist of more than one word. There are three conventions for combining these words:

  1. Glued together: getnumberoffiles
  2. Separated with underscores: get_number_of_files
  3. Camel case: getNumberOfFiles

Most Languages choose one of these variants. PHP uses all of them.

YouTube is almost entirely written in Python

http://youtube.com/results.php
http://youtube.com/my_videos.php
http://youtube.com/my_account.php

It is believed that Guido is referring to the YouTube backend. It’s also possible Google forced a rewrite, as I don’t believe they support PHP on their server farms.

The problem is php originally stood for Personal Home Page. And you can see that in the design. They have been adding one layer of functions on top of the other. This doesn’t make it a bad language. It just makes it a badly designed language. If you compare it to C# or others. You don’t get the feeling of consistency. Especially when you go into the OO interfaces.
Personally, I used it for big projects because it was the only real thing around a few Years ago. Today I would never put up with this mess. I shouldn’t have to care about what SQL backend I use. Even with Cake it is just another layer on the onion.

I liked this…

http://www.oreillynet.com/ruby/blog/2007/09/7_reasons_i_switched_back_to_p_1.html

The guy learned MVC using rails but didn’t like all the “magic” rails does under the covers so he switched back to PHP and re-wrote his app properly.

I remember the Facebook code leak, which is also coded in PHP. The code was clean and well-engineered.

It’s only a matter of who is programming. You just need some discipline.

Good post. At least an interesting read. I’ve been a PHP dev guy on and off for a couple of years now, and I’d just like to point out that I think a distinction needs to be made between a languages ABILITY to be written in nice, neat, OO style, and the general trend of developers who CHOOSE NOT to write it that way.

Bet I can write some C, Java, Ruby, or Perl that looks way uglier than most of the PHP you’ve seen - that is, if we’re nitpicking the way code looks.

While I was a bit offended by the fact that saying a programmer with even an ounce of talent would find that PHP sucks basically translates to that I must suck because I don’t think PHP sucks… I can let that pass, since I’m sure it wasn’t your intention to offend anyone :wink:

Anyway, while it’s true that a lot of PHP code sucks, it’s definitely caused by - as highlighted by the other commenters too - the fact that most PHP programmers are hobbyists and “amateurs”. I can admit that I was one as well, and my PHP code was as bad as anyones. Today it’s a different story, though, as I’ve actually been coding for so long and in different languages, read books (incl. some that are on your recommended reading list) and so on…

I really like languages like C# and Python, but I can’t say that PHP sucks. I’ve been coding in PHP for over 6 years, which makes it the language I’ve been using actively for the longest time. After you’ve been working with something that long, it all actually starts to make sense - but I think that PHP’s problems, such as function naming and parameters being inconsistent, and the problems they cause are exaggerated a lot.

I agree with the first part, not so much with the second. Why do I think that my opinion matters? Because less than half a year ago, I’ve consciously chosen PHP for a web application framwork I’ve built. All things considered, PHP was the right choice for the job and the resulting application works very well, has a very clear architecture, is extensible, efficient, separates concerns, models MVC, and generally does all the Good Things that you’d want in an application.

And still, PHP sucked. And it DID matter. A lot. How did it matter? I claim that, had the language been better designed, writing the application would have taken half the time. That’s right, half the time. Costing half the money. Which, for a small start-up, is a lot.

Saying that “it doesn’t matter” seems awfully cynical. Of course it matters and I very much regret that I had to spend any time whatsoever working with that defective tool. The thing is, one of the biggest reasons to use PHP (and the main reason we used it) is its wide spread. If you want to run your application anywhere, you’ve got no choice but PHP. This actually makes me quite mad: I’m mad in part at the inertness of the internet providers who are unable to provide clearly superior tools in their services, and in equal part at the PHP developing community who expose us to such a sucky tool, and who persist in developing new versions. Guys! Give us a break, stop developing that crap and force the providers to switch to better tools!

It’s easy for me to lay the blame at someone else’s feet and rant at them but this doesn’t my anger any less justified. PHP is badly designed and this fact won’t change in future versions.

building a compelling application is far more important than choice of language

PHP is cheap. The cost of entry for creating a web app with PHP is something like $1.99 a month + a domain name. You could probably create Digg by copy-and-pasting code snippets found around the web.

If you have an idea, PHP gives you a quick and dirty 1.0 (a la 37signals) implementation for cheap.

My question is how people let a profitable version 1.0 turn into a bloated spaghetti-code 2.0 without at least considering a more elegant and robust language.

The problem with say, cleaning up the functions and putting them into the right namespaces and libraries, is of course that you’re going to break legacy apps - unless you allow for the old functions to be used next to the new.

Then, you have to simply force everyone to rewrite their tutorials and books; because when a function is aliased, it does not scream “I AM DEPRECATED, STOP USING ME YOU IDIOT”, so people will just use the old one.

Simply by showing that message in a bright red bold font, I’m sure you could solve issues really quickly ;). ASP.Net truly crashes and burns with its Yellow Page Of Death; while it’s really handy sometimes that PHP doesn’t, it also allows the programmer to be a bit more lazy.

Oh yeah, either make the links of the functions point to something useful, or remove them and just color the font - right now every single one of them links to your archive ;).

http://video.google.com/videoplay?docid=-6304964351441328559 re: the youtube architecture. They switched to python a long time before google came knocking. They also use C. But i’m not nitpicking because for the most part you are right.

PHP has its abilities, no doubt. However PHP’s ability to scale - code length wise, is just not right. The later PHP’s are definately getting better. But look at PHP3. It gets messy quick and thats just the nature of it.

I dunno. I’m sick of these debates.

My question is how people let a profitable version 1.0 turn into a bloated spaghetti-code 2.0 without at least considering a more elegant and robust language.

Yes, I can’t possibly imagine how that would happen!

http://www.codinghorror.com/blog/archives/000256.html

I love PHP. I don’t know why people don’t like it, but I don’t use the frameworks of the day and engineer everything myself, so my code is elegant and there’s an easy transition to blocks when coming from Java and for some reason I just love weak/dynamic typing when working on the web (everything IS a string in the end).

I have to disagree a little bit. Right now I am working on a small website and a backend admin interface for a local theatre. I have never worked with PHP before and I am learning all the time. I use the Code Igniter framework and the experience has been quite pleasant so far. Example from one of the “views”, that produce HTML output from data supplied by the controller:

| if ($acts-num_rows () 0)
| {
| function acts_to_str ($i)
| {
| return anchor (“admin/play/show/{$i-play_id}”, $i-title);
| }
|
| entry (‘Acts in’,
| ul (array_map (‘acts_to_str’, $acts-result ())));
| }

This will produce an ul list of plays an actor shows in, if any. I think this is quite nice. Forms work in a similar way.

| field (‘Description’,
| form_input (array (
| ‘name’ = ‘description’,
| ‘value’ = $this-validation-description,
| ‘size’ = 50
| )),
| $this-validation-description_error
| );

Yes, some programmers can write FORTRAN in any language. I also came to the conclusion that it is possible to write LISP in almost any language. You may then object why not use LISP in the first place. Well, it is not always possible. This particular client has very tight budget and cheap hosting in my country (Czech Republic, but I suspect this is the same everywhere) means MySQL and PHP.

Has no one else seen the rails crap that is in some of the open source projects? Jeff you are correct, you can write crap in any language despite best intentions.

Imagine most novices trying to write a web application without rails or a framework - thats what most php developers are doing. Its quite an impressive feat when you think about it, what other language is so simple to prototype in? With rails you have to go through many, many more steps to get a hello world, and most non-developers won’t bother.
Plus, php is installed on every server on the planet.

Make your PHP suck less using this miniscule (60 line) MVC engine; I’ve used it to develop the web UI on an embedded device manufactured by Cisco, for TimeWarner:

http://code.google.com/p/barebonesmvc-php/

What personally makes me laugh in my mind is how everyone always says how PHP is so badly designed etc., but never can actually show how it slows down their app development. I think PHP’s manual is stellar, and it’s very easy to find a function for the job and its parameters and such…

No one has really been able to properly argument why it is that PHP sucks. The usual issues are always:

  • Magic quotes and register globals: anyone who knows what they’re doing can turn them off.

  • Function naming: while they may be a bit inconsistent, the functions for a specific area (like array manipulation) do follow conventions ok and should not pose a problem for a talented guy (the manual as I mentioned, a good IDE will show the params etc.)

  • It just sucks: omigosh, really? =)

What? Oy! That hurts. I guess now I know how it feels to be a fan boy (or perhaps zealot).

Perhaps it is due to my inexperience with other web languages (with the other being the ASP.NET) but I wouldn’t call it a bad language. Certainly, as bad as PHP scripts are, I’ve seen nightmare code born out of undergrads doing ASP.NET. One might argue that’s the undergrad’s fault and that they are not really programmers but people using WYSIWYG.

And I argue PHP has the same problems. It’s easy enough in that you can hack something up and it works, and given the surge of internet pages, these people may not even be interested in anything but getting the job done. Skip OO theory. Heck, they might even skip functions as merely being “useful”. That’s why there’s ugly perl. And that’s why (probably) there will be ugly code for all the new and hip languages (once it get popular enough)

I don’t see why good code can’t be written in PHP even by just someone who learned proper programming concepts. It’s just that PHP is too mainstream. It’s the myspace of server side scripting. Perl is probably livejournal. Python and Ruby is probably Facebook or Pownce. (Metaphor falls apart because it suggests there’s a lot of functionality differences).

Those who are really interested in programming flock to the ‘fun’ new languages. Those who left are those who probably don’t bother improving themselves and newbies.

I suppose I should be showing pure coding examples, and this may just be irrelevant but I do really think that PHP is not bad and I kind of like it. And maybe I’m just a little nutty and that I love programming so much that I even like the worst one.

Notice a pattern here?

Yes, but not your one.

PHP hosting is free and everywhere, that’s why we see so much PHP apps out there. Now take Python, Lisp, Ruby, Haskell, etc. web apps and you’ll have to pay for hosting.

Wikipedia, Digg, Wordpress can easily be done in thoses languages too (even with a clearer code I suppose). We only have to wait to see free hosting companies supporting these languages and PHP will not stay on top for sure!

No, we all hate javascript because of all the subtleties in different browser implementations. We all hate html for the same reason.

Maybe installing PHP has become easier, but I don’t recall it was ‘easy’. A real pain when you have to verify PHP on the machine is set up to the applications needs. You had to know what modules were in use and get/compile them if missing. Failing to do so broke your php app. When you went to upgrade, you had to remember the union of all modules in use across all applications and hope that there were no breaking changes.

No better than COM objects in classic ASP land, but it’s worth mentioning to the rosy glassed crowd. It’s called DLL hell for a reason.

I don’t use php anymore, so maybe it’s better now as more things get folded in. Heck, that’s probably why there are so many things folded into the mainline trunk.

RE no framework … PHP is a framework. How does it have $_GET and $_POST for variables in html request/responses if it’s not got a built in web framework. ASP.NET is a framework for web pages in .net, but C# itself doesn’t have a framework. ASP.NET doesn’t force n-tier, but it is capable.

n-tier is just a logical way to separate responsibilities, it’s not anything a computer cares about when executing code. It’s a layer of abstraction for the monkeys on the keyboard.