I Like PHP
"I don’t even bother writing tests for most of the PHP I write (obviously, I would be a bit more thorough if this code were destined to be used in a more mission critical web site). Not writing tests is not the only software taboo that I break when I write PHP: I happily mix up presentation and logic all the time. That’s just how PHP is supposed to work"
And this is why everybody else hates PHP. It tempts you to write bad code, and so most people working in the language end up doing so. I have to periodically stop myself when I'm writing PHP and make sure I'm not falling into bad habits. It's possible to write good code in PHP, and I know some people who do, but the pull to write sphpghetti is too strong for most. And there is no real corresponding benefit (aside from "noobs who don't care about code quality can still write programs") arising from the design choices that cause this.
There's a regular stream of "defending PHP" posts but they all seem to miss the point.
No one bashes PHP because it can't do these things, or that it isn't easy. People bash PHP because there are objectively better languages and platforms out there and objectively bad decisions made by those guiding the PHP language.
If you want to defend PHP in any meaningful way then you can't just list things that can be done easily in almost every semi-modern web ecosystem, but actually compare it to the other alternatives and identify when it's the right choice. It's harder but it leads to a blog post that might actually benefit readers.
> PHP is exactly like C. Either you like both or you don’t like either, there is no claim you can make about PHP that can’t be made about C as well, and vice versa
* C is fast and has tight resource control
* C is portable
* C has a transitive (==) operator
...
From the comments: "PHP is great for its purpose. Quick scripts that generate web content. It’s even OK for small applications. But just like C, PHP is lousy for the medium to large. Any software project with a duration in months or greater that is entirely in PHP is going to be painful."
Like Facebook? Or Wordpress? Or Digg?
PHP aint perfect, but it's demonstrably suitable for medium to large projects.
> PHP is like C
In all the bad ways, and none of the good ones.
My first web language was PHP, I started working with it 12 years ago. Back then you didn't have Rails, Django, ORM, unit testing (in web dev that is), and javascript was looked as a language to just show popups and have confirm()'s before submitting forms.
If you wanted to connect to a MySQL, you have to do it all by yourself, you need to create a database, tables, PKs, connect to database, execute RAW mysql queries, and so on. Form validation ? Yeah right. Back then the include_once() or require() were amazing for separating code. But all in all that mades us learn how things work internally, how HTTP requests work, how SQL works, even why CSRF or XSS and SQL injections are executed.
Yes, the code was shitty, it was all over the place, but we learned how a web app works internally.
And yet, most developers I know who coded PHP back then are amazing developers today in any web language or framework. Why ?
Because most of them learned doing things the hard way. We had to understand how HTTP requests works, how SQL works, etc..etc.. because we didn't have the magic of rails, or any helper for any framework.
In these 12 years, web development as grown, a lot, and for the better, I'm currently learning Rails, and I've already done a project with Django and Node.JS . I liked them a lot and they are amazingly well done, I just wished PHP was more OO like ruby or python.
However, I laugh at these new Rail/Django developers that joke about PHP code. Most of them start coding jumping immediately into a framework. I bet that most of then couldn't create a simple MVC framework, ORM from scratch, hell, even a simple security CSRF would be a pain without "gem install <whatever>".
You see kids, PHP was like our first GF or our first car, where we tried stuff, made us cry (in a sense), and helped us grow and mature as developers, for those who wanted to grow that is. Now you have lots of languages for web dev, but back then PHP was the thing.
I still believe that PHP is a very cool language if done correctly, I wished tho, that it should be rewritten to be more OO.
But if one thing I learned from all of these years is that, it's the developer who makes quality code, not the language. Unless that is, you are using a framework who basically demands you to do things the way it wants, and puts a lot for magic so you don't bother with "boring" stuff. But in the end of the day it's not your code, it's theirs.
So my advice for new dev is, forget frameworks, and learn the hardcore way, then pickup a framework you love and go with it, either PHP, Ruby, Python or whatever.
"There is nothing more exciting than modifying a file, hitting Refresh on your browser and seeing the result right away"
I think this feature is definitely useful, but you can virtually do the same thing in other languages with templating engines.
Where I work, we've standardized on PHP and I have no choice but to use it. It isn't as bad as people say that is! Our organization's product is a large application written in object-oriented PHP with unit tests for every method of every class. As a team, we use continuous integration and have regular code reviews to promote quality. We've done some things that we're not proud of, but on the whole our codebase is pretty clean and elegant, and at the end of the day I'm almost always proud of the work I've done.
What worries me about PHP is that innovative ideas don't appear to be coming from the PHP community. They come from Python, or Ruby, or Erlang, or Java projects and are copied by PHP developers afterwards. I worry there's a risk that PHP is being left behind as the developers who solve new and challenging problems turn to other languages.
Furthermore -- and this is just my sense of how things are where I live -- while it's possible (and not even difficult) to write quality PHP code, it seems to be difficult to find developers who care about quality. My manager complains about this constantly and I'm inclined to believe him since, when I was a freelance developer, I saw tons of PHP code that was simply and unambiguously terrible. Conversely, a developer who wants to write quality code in PHP has to look pretty hard to find a PHP shop that recognizes quality and values it. I suppose that these issues aren't tied to a particular language, but they seem more pronounced with PHP.
PHP's greatest strength is that it doesn't require OO. Seriously, for most simple web application tasks (and even some complicated ones) OO adds more confusion than it removes, and I say this as someone who certainly sees the value in OO for complex apps.
Most web apps are ultimately more like shell scripts or console applications than desktop/mobile applications. It's no coincidence that most of the former are written in C or C-like scripting languages, and most of the latter are written in OO languages running on .NET or Java. Your basic operations are "print some stuff", "read from a file or database" and various sorting, slicing and text-processing tasks inbetween, all of which are done well by PHP, Perl, C and so forth. ActiveRecord, TableDataGateway etc. are great patterns for data access, but sometimes mysql_query("SELECT foo FROM bar WHERE x = y") is just so much simpler that it becomes the better option.
The lack of OO makes PHP's documentation much more browseable, and makes code more readable in certain situations. If I look at your code and I see a function I don't know, I just google it. If I see a method invocation on an object, I need to figure out the type of the object (not easy in a dynamic language) and google the combination of class and method name, which often fails to return a result (has anyone ever managed to find anything useful in the Zend Framework docs?). PHP isn't the only data point for this argument - I think it's probably why Drupal handily beats ZF, Symfony etc. in terms of popularity, despite the superior OO architecture of the latter. You can google pretty much any API function and get an accurate result, which you just can't do with ZF (which lacks documentation at the method level, and what documentation that exists is buried in some Doxygen-based site which is made impervious to search by the use of frames). I'm also reminded of Linus's argument against OO in the Linux kernel - when you're all about the patch files, you need clear function names to understand what's going on without massive amounts of context. Drupal's rate of contributions probably exceeds that of other frameworks for similar reasons.
Of course, this bias against OO is also what makes PHP a bad choice for certain things, and this is why plenty of devs hate it. I honestly don't know if, in the final analysis, this makes PHP a good or bad choice. It probably depends too much on the problem you're trying to solve for there to be a simple answer.
This comment is almost better than the blog post itself:
> But just like C, PHP is lousy for the medium to large. Any software project with a duration in months or greater that is entirely in PHP is going to be painful.
Yeah, C is terrible for medium to large projects like, say, Linux.
I'm just glad we're at this point where there are so many quality languages and frameworks available that we can fuss over which one is best. That is all I'm going to say on the subject.
> I write PHP code very sporadically
Explains it. PHP is fine if you jump in, write a few lines, and then don't ever have to maintain it yourself. Woe to the guy who does have to maintain it.
I like PHP because it's got a C-like syntax, which is a personal preference of mine. I also like it because it exposes the nuts-and-bolts of web development without any fuss, unlike, say, ASP.NET (though last I touched that was 3 years ago so doubtless things have changed since), and that it has a basic templating system built in, and that it's easy to install and deploy to.
It definitely is deeply flawed for many reasons, which is why I'm still looking for my "white unicorn" language: one with a C-like syntax, no fancy templating or ORM magic (just let me print() html), foss, easy to deploy, and with an established community and searchable "recipes" online.
Lots of languages fit some of those criteria, but I haven't found one that fits them all.
Stopped reading when I came to:
"Sometimes, I don’t even bother editing the files locally and then transferring them: I ssh to my server and modify the files live."
More than providing any defense or justification for the use of PHP, this article greatly bolsters the opinion that PHP is for noobs.
You can do anything web related with PHP, it might not be the coolest kid in town but it gets every job done pretty well in the space it has been designed for. Use a framework like symfony and large scale applications with decent design are no problem at all in PHP. Same is true for Python, Ruby etc though, so just use what suits you best. Comparing it to C ? Dunno, completely different animals. I wouldnt do web dev in C, but i also wouldnt do graphics programming in PHP. Just because they both are not designed with OO at the core doesnt mean they are exactly the same.
My observations for the popularity of PHP are this:
1) It's old. You're alternatives to PHP 12 years ago are all bad. The most popular alternative was perl CGI scripts.
2) Since it's templated, it feels more like markup . Someone with absolutely no programming background can go: HTML -> HTML with a javascript "onclick" handler pasted from the web -> HTML with a bit of PHP pasted from the web -> writing my first original PHP code
PHP's one great strength is that it can be effectively used in a very wide range of applications. But doing that well requires using the language in ways suited to that purpose.
The problem with people "defending" PHP is that they often only use PHP for a very specific purpose, and describe PHP from that perspective.
Hacking quick and dirty procedural scripts, writing something like WordPress plugins or developing "enterprise" applications with frameworks like Symfony or Zend are entirely different ways of using PHP, usually done by entirely different kind of developers.
The unmaintainable crap PHP is infamous for occurs when less than mediocre developers only familiar with one approach apply those limited skills to a project that requires a completely different approach.
What the author describes is not "how PHP is supposed to work". It's just one of many ways PHP can work. (And certainly not my way of using it.)
The biggest problem with PHP isn't anything built into the language itself, but rather the culture of mediocrity/naivety surrounding it.
As at least one other commenter has said, there are disciplined and skilled PHP programmers. If only they were the majority! Sadly, for the PHP ecosystem, a few good apples don't unspoil the bunch.
Here are a few ways PHP's bad culture manifests itself.
1. Cargo culting
The web is filled with questionable recipes for doing this or that in PHP. I've observed a strong tendency of PHP programmers to approach every possible addition of functionality like this: 1) Google search; 2) find source code that supposedly does what's needed; 3) paste that source code in without understanding it or thoughtfully evaluating its appropriateness.
I'm all for learning by reading other people's code. I'm even OK with pasting code when you understand it fully. But cargo culting is bad, and it seems rampant in the PHP world.
http://en.wikipedia.org/wiki/Cargo_cult_programming
2. Lack of architecture
PHP applications often lack a coherent architecture. It's a typical mistake of new programmers--one that I made many times--to think just enough about architecture to accomplish the task immediately at hand. Naturally, this comes back to bite you when you want to extend your code's capabilities. I see this all the time in PHP applications.
Consider Wordpress. Its API is a random, ad-hoc mess of global functions. Things that should logically be exposed as objects aren't (like posts, for example). The API for retrieving content is different depending on whether you're in the mysterious "Loop" or not. And so on. A more detailed analysis of the lack of architecture in Wordpress can be found here:
http://www.phpvs.net/2009/12/08/an-exercise-in-wordpress-int...
3. Frankenstein coding
Similar to cargo culting, Frankenstein coding is when you cobble together an application from a bunch of off-the-shelf plugins or modules. This is a common practice in the Drupal world specifically, but it applies to a greater or lesser extent in much of the PHP ecosystem.
There are two major problems with this approach. First, the chunks of functionality you download are not likely to match your needs perfectly, so you're forced to make due with software that's almost what you want. Second, the code quality of these modules is often abysmal, and that means security flaws, among other problems.
Sadly, many PHP developers seem perfectly content with this. Even worse, they do this for clients, who get a semi-functional end product at bargain basement prices. That practice gives me a cheap, sleezy vibe, and it doesn't help my impression of the overall PHP ecosystem.
The language itself is so-so in my opinion--not good enough to be my go-to language, but not so terrible I'd refuse to work with it. But somehow, probably due to the language's accessibility, the PHP ecosystem seems to have been swamped by coders who know just enough to get by. And that's my real problem with PHP.
PHP sucks as a language, but honestly, it's a LOT less painful to develop a working web app in PHP than Java, Python, and just about everything else.
Why? There's no deployment issues. There's no waiting for my app server to restart, no delay while a change is detected, etc. You edit a file, click refresh.
Flame away.
PHP is fast to develop with, but its inconsistency and quirks make it a real PITA to work with quite often. a good summary of the main crap is outlined well at http://www.phpsadness.com/
Keep in mind that perhaps a majority of wordpress.org sites that have been around for a year or more have been hacked in some fashion at some point, and that the reason is because PHP encourages the kind of terrible code and unsafe-by-default environments that make this possible.
Edit For example, I am thinking of how various templating languages, including Jinja2 (for Python) default to auto-escaping strings you're echoing, but with PHP, you by default have to explicitly think about it and call `htmlentities($x, ENT_QUOTES, 'UTF-8')` all the time.
From TFA:
> * I have yet to see this kind of universal support for any other language than PHP. Not even Ruby on Rails, let alone Java, is available on mainstream providers, thereby validating the claim I made five years ago that Ruby on Rails won’t become mainstream (I regularly receive emails about this article asking me this question, and I keep responding “Nope, still not mainstream”).*
If your provider doesn't support Ruby, you need to get yourself a $20/month Linode box. If Linode and a $20/month price point isn't mainstream, I don't know what is.
>Either you like both or you don’t like either, there is no claim you can make about PHP that can’t be made about C as well
Except that PHP is terrible for systems programming?
Is PHP that much worse where it's worth it to jump onto the ruby ship?
I'm just starting to code one of my current projects, but in PHP. Should I consider ruby instead?
I submit that PHP's popularity has much more to do with mod_php and its ubiquity across cheap web hosts than any intrinsic merits as a language.
As someone who made a good living for many years by building stuff with PHP... I couldn't get away from it fast enough. Rails+Heroku is the new PHP, if you want a batteries-included web framework and easy hosting.
Not rhetorical: does anyone here use PHP for starting new projects?
"there is no claim you can make about PHP that can’t be made about C as well, and vice versa."
What about hundreds or thousands of inconsistently named built-in functions?
C doesn't really have built-in functions, just a standard library that you don't have to load if you don't like it.
The core of C is small enough that you can fit it in your head.
PHP has so many functions that it's almost impossible to write it without googling php.net, etc.
Not that this matters so much, Google is fast and omnipresent, but in that sense they are definitively not alike.
> Not even Ruby on Rails, let alone Java, is available on mainstream providers
Just picking a few of the "big" ones:
dreamhost.com: supports RoR
hostgator.com: supports RoR
bluehost.com: supports ROR
hostmonster.com: supports RoR
godaddy.com: partially supports RoR (fastcgi)
I guess that is simply not true anymore
One of the reasons I like PHP is that it still lets you get to a lower level of coding. Many of the newer languages and frameworks that have become popular in the past couple of years abstract things such as database connectivity away (Django is a good example of this) and it makes it difficult to optimize queries effectively without hacking onto the framework.
me too, i like php. it's a decent templating language with some very advanced features. it even supports namespaces!
You know who used php? Hitler.
Gotta love these "My tool is bigger than yours" discussions...
You say if you make one assumption about C, you have to say the same about PHP. Well, PHP is academic, C is not. C is fast, PHP is not. I would only have agreed with your post if it were PHP and C++. C++ is academic and it's not slow but it is slower.
You also say that you either like both PHP and C, or you like neither. Well what about Lua, Python... or any other embed-able language? Most Python programmers would say they don't like PHP but love C, because the languages work well together, same with PERL as well. You could say that you either like both C++ and PHP or you like none. Maybe, because PHP can compile to C++ to become extremely fast, which is why Facebook and Yahoo both use PHP because it can be fast and it is well documented. It should not be compared to C though.
(Note: I contradict myself, PHP by itself is slow. PHP is not fast whatsoever. C++ is a low level language which is the only reason why it's fast and I think it's awesome that PHP can compile to C++. I'm in no way against PHP and I'm not encouraging people to stray away from the language. I was just pointing a simple error in this post.)