PHP Sucks, But It Doesn't Matter

I agree with Jon’s comment above, about PHP being a “WOP” (Web-Oriented Programming) language. People often ask rhetorically “But why is PHP so popular for Web programming?”. Clearly, it’s not because PHP is elegant from a formal language design point of view.

Just take a look, for example, at O’Reilly’s “Learning” series of books. Compare “Learning Perl” and “Learning Python” with “Learning PHP5” (I’m doing it now using their Safari service). The first two barely address Web programming at all (at least not explicitly on the contents pages) and are clearly general purpose programming/scripting languages spiritually at home on Unix and derivatives. The PHP book, on the other hand, has a few chapters on language basics, and then gets right into processing forms, connecting to databases, managing user session, processing XML and uploaded files, etc. It has appendices that include information about configuring IIS and Apache, and the book doesn’t assume its audience is is using or deploying to Linux or BSD.

This difference of emphasis is played out across the publishing industry and online resources, it’s reflected in the nature of the advocacy for the respective languages, and consequently I don’t find it surprising at all that PHP is used so frequently for web programming.

My main gripes as a PHP dev: no console and no first-class functions. I do like, though, that the data structure in PHP is an ordered dictionary. If the array functions were namespaced, “import array” would be as cool as “import antigravity”; you get a queue, a stack, a dictionary, and a list in one.

Just because it’s easy/possible to create spaghetti code doesn’t mean you can’t create efficient and maintainable php apps. My apps are object oriented, business logic is separated from the html, and I probably only use 5% of those ‘a’ functions.

Funny, I’ve written a PHP Sucks article myself (and got lambasted for it) but I’ve put together one site in PHP and am starting another one and yes PHP still sucks, but you can get a lot of work done in it. I debated Java vs Ruby for my new site for weeks and finally decided, I’m tired of debating with myself, just get it done in PHP.

At my very first ever job as a Professional Software Developer™, I had to maintain a PHP app. I sometimes have nightmares about it.

Why is pascal getting popular again?

I’m not going to make dumb generalizations about everyone who writes code with PHP but it does seem that the way the language is designed really does encourage sloppy and procedural style code. I believe that better language design would help a lot with the quality and maintainablity of PHP projects that are available.

@steve:
Some languages are bad because they limit, restrict or force your choices. Take the example of namespaces. In a language without namespaces, I have to find a way to avoid name collisions. In a language without closures, you can’t pass code encapsulated in a closure. That forces you to solve the problems using a solution you consider not to be the best. This is where some design patterns might help you out, they are a cristallization of a solution for a problem on a unique domain (example being multimethods and the visitor pattern).

OTOH, the more power you have the more rope you have to hang yourself into.

The only conclusion I can draw is that building a compelling application is far more important than choice of language

Pfft. PHP is good at what it does, and sucks at what it’s not designed for.

It’s like claiming PERL sucks (merely) because it has a very dense syntax, or because it is terrible at high performance computing.

PHP was designed to embed code inside of HTML pages, and it does it very well. tag tag tag… “Hello ?=$name? how are you today? I see you have ?=$wallet? dollars left in your wallet.”

It’s fine.

Languages don’t much matter. Programmers do. Garbage in, garbage out. A language doesn’t magically transform your application into a work of art.

@Mecki

That defense is old in the tooth. Who cares if it’s the “language” or the “API” - we all know what Jeff is talking about. Even the pro-PHP comments here admit that there’s something less than ideal in the PHPsphere.

Besides, can’t the problems in the library potentially be attributed to the language? I mean, if you want to draw lines in the sand and make the argument about language vs library, PHP is a good language, but is it a great language? I mean, no closures, no pure OOP (maybe you consider that a benefit, i don’t), no mixins (i have seen hackish implementations), namespaces, late static binding (coming soon), horrible scope resolution (you need $this- infront of everything…)

So even if you are right that we’re all mixing library from language, it doesn’t magically make all the problems of both the library and the language go away.

PHP Bringing order to the madness: http://www.qcodo.com/

i think the great skill with languages like php (such as ‘classic’ asp) is to “program into the language” not just program in the language.

This is a Steve McConnell idea – he harps on about it on every second page of Code Complete.

i’ve seen some very well structured, somewhat beautiful classic asp – and it shows great discipline on the part of the developers.

lb

Since I was quoted in this post, I figured that I would make a contribution to the steady stream of comments.

In my opinion PHP sucks because:
a) Incredibly inconsistent naming
… 1) Mixing of underscored and non-underscored
… 2) Inconsistent abbreviations, e.g: using “i” or “case” to imply case-insensitivity

b) Somewhat inconsistent argument order
… array_map(func, arr) vs. array_walk(arr, func)

c) Expression de-referencing
… Meaning doing something like: “echo (array(1,2))[0];” will fail miserably. Except that it works for object references… IN SOME CASES, could it get any more inconsistent ?

d) No (proper) support for lambdas or closures

e) No support for namespaces, yes it’s coming in 5.3/6 but it should’ve been there from the beginning - in maybe 3-4 years we will be able to use namespaces in production code.

f) Usage of array() instead of a simpler syntax such as [] or {} for creating arrays, which makes it incredibly ugly and hard to read when you need to nest several arrays

g) Usage of - instead of . as the object operator, this is actually more a convenience concern then aesthetic, writing - (at least on Swedish keyboard is a PITA)

h) Incredibly old fashioned object model, it got a worse feature set then Java had back in -96. They’ve tried to launch PHP as an “OO”-scripting language, right.

i) A lot of functions that mostly do the same thing, takes the TIMTOWTDI to a whole new level.

There are a couple of more issues for me personally, but these are the nine main ones. And all the issues with the “hobby” community, etc.

So yeah, PHP sucks. However I still use it because it puts food on my table and pays my bills.

This same argument was once used for COBOL. PHP will eventually be replaced as COBOL was. Sad thing is, it will take very long. Even today COBOL struggles on some reincarnations.

Isn’t the classic “but you write PHP that doesn’t suck” example Drupal? It usually comes up in these kinds of discussion anyway.

It’s patently clear very few people posting comments here (or Jeff) has used PHP any time recently. There’s no other explanation for the foolish and incorrect things stated here.

I used PHP in 2001. It was slapdash. Using it as a comparison is called a “straw man.” Boo.

I use PHP in 2008. It’s got everything you need to build stable, well-designed, scalable apps. Period.

The only legitimate complaint I have ever seen about the language itself is lack of namespacing. Welcome to PHP6.

There’s nothing wrong with the language.

People who use it poorly have themselves to blame.

As someone who codes VB6 and PHP pretty much exclusively (though I have gone on to VB.NET, 'cuz it was free and all), I just want to say that my goals are never to be elegant. My goals are to be quick. I am an experimenter, a tinkerer. I am not a computer scientist by training. I am a historian who can code a bit.

Having languages like VB and PHP allows someone like me – who doesn’t want to do memory management or worry about callbacks and etc. – to quickly throw together things that other people won’t. Can I beat the computer scientists when it comes to writing efficient code? No way. Can I beat them when it comes to writing code I can use? That’s the whole point.

So what’s my point, in the end? The people who bash PHP aren’t taking into account the crucial social aspect of it. Why does it matter that Wikipedia and Wordpress are written in PHP? Because practically anybody can work on them. In a land where processor time is cheap, the ability to quickly modify an existing program is king.

I write my own database interfaces in PHP because the off-the-shelf programs for historians, like EndNote, suck a big one, in my opinion. They aren’t set up for me and they can’t be customized to my needs easily – something that’s a big problem in the humanities, because my work doesn’t fit into nice little boxes each time. Each project has its own needs and considerations. A well-structured program is, well, a little too well-structured. Something made out of PHP can be mashed about, changed, new things added quickly, etc., on the fly. And it works.

Should an entire OS be made out of languages like this? Should critical programs be made out of languages like this? No way. Should programs that place a high, high priority on being easy to edit by people without CS degrees be made out of languages like this? Yes. And of course that’s going to bug people with CS degrees – PHP makes those of us without them think we can nudge you out of the picture. :wink:

PHP is successful for exactly the same reason VB was successful: they both made a simple thing very simple while no-one else cared.

Unfortunately both sucked in every other possible aspect, thus repelling most good programmers and attracting hordes of bad/amateur programmers, building a huge environment of crappy coding standards.

And unfortunately PHP still has a big selling point: want to do a web app that you can install everywhere? Do it in PHP.
And so we are stuck with PHP for a long time.

Also, it is true that a bad programmer can write bad programs in every language, but the reverse is not true.

  • If you don’t have namespaces you are stuck with adding ‘whatever_’ in front of all your functions,
  • if the language doesn’t have a consistent naming convention you can pick one, but you will still live in the mess
  • if (for example) you don’t have function pointers or first class functions you are stuck with doing the big ‘switch’ statements

You can make bad code with anything.

And the converse is also true: you can make GOOD apps with anything – that’s the point of this post.