83 comments

[ 3.0 ms ] story [ 132 ms ] thread
I love picking on PHP as much as anyone else, but you can write just as terrible code in Python as any other dynamically typed language. Those Django and Rails projects can grow into monstrosities as well.
Of course you can, but I think choosing between various languages mostly ends up being a matter of taste. I tried PHP, built a rather large website using it and found out I didn't like reading my own code after a while - a problem I have not encountered yet using python. I don't think this kind of article should be taken as a list of reasons that make python better than PHP, just as a reminder that maybe you could have a look at python if you do not enjoy writing your code in PHP.
Perhaps, but while the PHP community will debate endlessly about standards and projects inevitably end up become a pile of inconsisten spaghetti code, the conventions in Python make uniformity the path of least resistance, at least by an order of magnitude.

Also, while Rails is in the same league as Django as far as tools and expressiveness go(expressiveness perhaps more so), Django goes to considerable lengths to enforce style and consistency; it is not taboo to politely tell people that their code is not PEP8 compliant.

While certainly not as strict as PEP8, the Ruby community also has accepted style conventions that all bigger projects follow (especially Rails) and that you can call out people on.
Instead of choosing one over the other, why not choose both? I specialise in both PHP and Python. For web projects on commodity hardware, nothing beats PHP, especially when used with powerful frameworks such as Zend, Yii or Drupal. For installable programs or scripting, Python works well with wxPython.

>> Quoted from article: "It was around this point that I decided I'd rather be an unemployed Pythonist than an employed LAMP developer."

Sorry man, I'd rather be an employed LAMP developer, but with a few side projects in Python. It's just a programming language, not a religion. :D

I'm the author of the blog post. If the question is related to why I dropped PHP it's because I was getting a lot more done in Django and Tornado so I could finish projects quicker and take on more work.

There were so many clients in need of Pythonists in London in 2011-2013 when I was working there that I didn't have the time of day to take care of PHP ones. Also, PHP rates were dying and there weren't a lot of people competing for the Python work so I could keep my day rates nice and juicy.

I can think of a lot of things that beat Zend, Yii and Drupal in terms of code management, security, DRYness of code that's implemented, flexibility, the list goes on.

As a fully employed Drupal developer who's currently spending every free minute working with Rails, it's vastly more about working with tools that I like working with, and PHP and Drupal do not currently give me that.

I can play a little piano, but I'm way better at the bass because it's more fun. I can get work done on a PC, but I get way more done on a Mac because it's easier for me to work with. I can cook with a dull knife, but it's never as much fun as using a good one.

I think it's okay for people who don't really like PHP to publicly admit that, but it often seems to be taken as a religious attack by those on the other end of the spectrum.

I'm taking a Coursera course at the moment where a lot of people on the forums are new to Python. I've been trying to guide them in the right direction in terms of the Pythonic way of doing things which has been a very interesting experience. Lots of people turning to for loops 5 levels deep (or even worse - wrapping 5 for loops in a list comprehension).

This is a typical quote:

"I'm still a little bummed that python doesn't have the for(;;) C syntax"

I'm pleased to report that quite a few people have been impressed with how clean and simple the code is once they see the Python way.

My girlfriend is taking the python course with them as well. She said there were 60,000 in total taking it (sounds like there will be a lot of new Pythonists in 2013).

She and another friend of hers was having some troubles understanding when and when not to use parentheses and I did a quick clinic with them to walk through it.

It might be a good idea if volunteers from the Python community could be found to offer extra lessons, perhaps some sort of live video cast mixed with a live chat. It could help out a lot of people who otherwise don't have many people to call on around them.

Cool - which course is she taking? This is the Computational Investing course. Buried very deep in a forum is a post by the tutor saying that you should know programming and how to use the command line - unfortunately it's too well hidden.

The main struggle has actually been with people on OSX Lion getting the libraries installed.

https://www.coursera.org/course/compinvesting1

On the introduction page of this course it states, "This course is intended for folks who have a strong programming background, but who are new to finance and investing."

Hardly well-hidden.

I stand corrected. Maybe there should be a test to weed people out early then. Dunno - there are a few too many people on the forums who don't know the 'cd' command.

I was referring to this little quiz [1] which I thought should be posted directly in the description to scare people off.

[1] http://wiki.quantsoftware.org/index.php?title=Compinvesti-pr...

Definitely a bit more computationally intensive than I would have imagined it to be. That's a bit of a challenge even for some with a CS degree!

Cheers for pointing this course out, though. It's right up my street.

Great! Very interesting stuff (as it's all new to me).
She's doing the 'Learn to Program: The Fundamentals' course run by U of T. I'm glad she did as it's forcing me to use Python 3 :)
Things I like PHP for would be single file simple scripts which are part of otherwise essentially static sites. For instance, a static blog (with Jekyll/Pelican/whatever) but a single .php file which is for searches (in a sqlite FTS cache of the blog, say) or 'related posts' or whatever. Then you can upload it into an apache "standard" web host, and add that tiny bit of server-side stuff that otherwise would be a pain.
For a lot of developers that single file explodes into a large multi-file monstrosity as features are added and deadlines are put ahead of refactoring.
Have a look at Sinatra and Tornado. Single-file projects are possible as well and it'll lead you down a road to quick deliveries and better tooling.
Yeah. Pretty much all new projects I do are python (flask, actually), but I still can see a place for very simple PHP scripts - which I think was it's initial intention, and which it can still do.

The same for sh/bash. It's great for very simple automation/scripts, but after certain level, it's much much easier to use a language which helps you write maintainable code and doesn't actively try to stop you.

It's not that they're not possible, but if you want an "upload and forget" way of deployment, you have thousands of hosting providers with a LAMP stack, but only a handful of Python/Ruby providers like Heroku (or you have the choice of using a VPS, which again means having to manage it).
What I got from this was - he ditched PHP not because it's impossible to write good code with it, but because it's possible to write crappy code with it. And that messed up code is result of bad team, not bad tool. But yeah - you can feel that I'm PHP dev and if that will result in HN down-votes, so be it. But I had to express my opinion.
What I was most hoping to get from this blog post is to encourage PHPers to give Python a go. Try it out, build something in it. I think lowering the amount of bespoke PHP code in the wild would help the software industry as a whole.
That or the referral codes on your links to Linode and Digital Ocean.
Yeah, god forbid he have links that generate revenue for him. For shame!
Good developers should try to learn new tools regardless IMO.
This right here is the best comment in reply to the original article, and says succinctly why the original article misses the point.

Now that he's classifying himself as a Python programmer, he will run into the same problems as a when he was just a PHP programmer.

Language Programmers suffer.

I appreciate the encouragement, and a couple references along the way. After some time away from programming on a daily basis, Python is bringing me back.
I would go farther and say PHP makes it easy to write crappy code. Much harder to screw up in Python than PHP. Can you do it? Yes, but have to actually go out of your way to it, as the "easy" way is 99/100 the correct way, or will at least will fail hard and fast. That is where PHP errs, and something Go gets right. Code should either work or error ... warnings are a code smell, and PHP is very warning happy, including things that are an error in pretty much any other language, like accessing an undefined variable.
Actually, this is the way I'd look at it too - speaking of someone who was guilty of being very 'jacky' in managing a PHP production codebase. Throw some code inside a function to deal with whatever issue you're having and call it good...

In Python, it almost feels like that is akin to changing gears without a clutch... and I've only theories as to why that is.

it's more like the abandoned PHP because they had a crappy years-old codebase. And start developing in a new language (Python) with a new codebase which is not years old and they can design correctly. And in about 4-5 years they abandon python because they've got a crappy python codebase...
So... OP worked on a team of people who didn't care about good code, and were using PHP. It probably happens with PHP more than other platforms, but I've worked on teams who did crappy Java and crappy Perl and crappy ASP too.

Bigger takeaways - working in a situation where you get to choose your own tools and be self-directed is probably a better fit for this person. When he works on a team of 3-5 other people who want to do different things in his Django code, there will be tension. Probably not as much as his PHP monstrosity, but there will be some.

Working with large established frameworks, it's hard to see one file have JS, HTML, raw SQL, ORM code and core PHP app logic all in the same file. The conventions dictate where/how to separate.

The OP likes that separation, likes conventions, and wants to move to Python. Great. However, I feel the benefits he's going to get are coming more from a greenfield setup and working alone vs the language or framework itself.

Also, typo alert: the word is 'resonate', not 'resinate'. ;)

Yeah, I'm getting the feeling that the OP has no idea of PHP frameworks (or anything that has happened with PHP in the last 5+ years for that matter), and prefers to throw the baby out with the bathwater because of this.
I was using PHP frameworks between 2007 and 2011. Have you been watching what has been happening with the Python-based frameworks in the past 10+ years?
Were you really? Because saying things like "There is package management in PHP but I never found any code bases in the wild" or getting excited by writing RESTful APIs paints another picture.
You've taken the wrong impression from those statements. Have a look at my CV if you wish.
To be fair, it's pretty hard to refactor existing spaghetti PHP in to any comprehensive framework, at least production systems within a company with deadlines and customers.
Is that something intrinsic to PHP? It's hard to refactor any code when you have no time allocated to maintenance.
I do think it's a bit harder in PHP, given the nature to which it allows for spaghetti code to happen in the first place. Certainly it's never easy in any platform, of course; I do think "bad" PHP is harder to refactor than "bad" other code. Just an opinion, of course.
Any particular reason? You can do fewer terrible things than you can do in Perl, C, C++, etc.
Indeed but it's ungodly easy to add composer, follow FIG standards and be on your merry way to refactoring. At some point if you keep it up it will be easy to transition into a good framework like Symfony.

I just refactored an old class hierarchical system where each class extended a random other one and all functions were called from a global parameter or $this-> so if I can do that, anyone can!

Python encourages you to write clean code.
Does it really? I'm not being pedantic. My experience has been that coding guidelines/style and personal standards are what encourage good, maintainable code. Not the language itself. I've seen beautiful PHP, just like I've seen horrendous Python.
Somewhat. The clean syntax of the language certainly helps. However, it's not really Python itself that encourages clean code. It's the Python way -- meaning the culture. The concept that there should be only one right way to do things is extended right down to the way code is formatted. There is even an official PEP (Python equivalent to an RFC) which covers it.

In other words, in the Python world, personal standards are discouraged, and there is a remarkable amount of voluntary compliance with the community standards. I work in a number of other languages on a regular basis, and while there are certainly a set of best-practices out there for most languages (often 2 or 3 of them, in fact), I don't see anything nearly so much uniform compliance as in the Python world. I don't really know why.

As I've grown more experienced with writing Python code, my interest in writing readable code has also grown. I would guess that most of the readability concerns come from graduating, as I started working with other people on non-disposable code.

I feel Python has played a part too though. Python has an official style guide (PEP 8), so code is more homogenous across developers. Python also has a set of core principles (the Zen of Python) which put a focus on readable code.

wanna bet that I can write bad code in python? :) :) :)
wanna bet I can write a much worse version of yours in PHP?
Exactly..take Symfony2 for example, which creates clean and maintainable PHP code using very modern methodologies. You have package management, ORMs, ODMs, Template engines, coding standards and a huge community that produce high quality Bundles (like plugins) that you can integrate into your project. Id even argue its better than Django or Rails nowadays.
Who didn't see this response coming? I'm sure it is possible to write decent PHP code, but it's funny how that never seems to actually happen.
It does happen - I've seen it multiple times, from people far more disciplined than myself.

As to this (my) response, I was pushing a bit more towards "greenfield projects always tend to feel better", but I'm not sure they'll be much better over the long haul if the same person with the same skill level has the same constraints (time, features, etc).

"Abandoning KungFu for Taekwondo". Both are means to an end (or survival).

I agree that there is a large number of bad php code/coders out there, but you can write and build beautiful, fast and well designed codebases with PHP just as well.

Like the martial arts, the barrier of entry is very low. The black belt, however, takes perseverance and years to earn.

I am not very familiar with PHP dependency management. If you are using Python, you should use virtualenv. Apart from providing you a repeatable development environment, it also enables you to use 2 versions of the same library in 2 different projects.

I deploy within virtualenv mainly because I use the same machine to host multiple services. I sometimes use requirements.txt.freeze(pip freeze -l requirements.txt > requirements.txt.freeze) for production deployments, but in the end, I find specifying versions in requirements.txt itself more convenient.

If you are building async services in python, have a look at gevent and learn the basics of event loops. gevent abstracts the event loops(there isn't an explicit loop) but there are cases where you are better off rolling your own select/epoll loop. Also, the article paints too rosy a picture - "I've built backends that do authentication, query a few million rows in Solr and return JSON in a couple of milliseconds thanks to Python" If you structure your applications properly, and your application is IO bound, you can get good performance out of Python. But it goes without saying that Python isn't a performance powerhorse. You might need to replace CPU intensive services with some other language or write a c extension or write cython ...

Dependency management in PHP is generally done through Composer.

http://getcomposer.org/

There is no analogue to virtualenv, and dependencies are loaded into a directory rather than somewhere in the operating system.

Personally, I think language wars are silly. Languages are tools. Learning different languages only makes you better prepared for the market and give you greater freedom to choose from a wider range of the available jobs.
I can't help but get the impression that the author has not kept up with recent developments in the PHP world.

Python has PEP-8. PHP has FIG. Not as ubiquitous, but its getting there.

"There is package management in PHP but I never found any code bases in the wild using it to the same extent as PIP". Wait, what? Just about everyone uses Packagist and Composer these days.

Also, isn't is pythonista, not pythonist?

By the way, no disrespect to Python, which I use as well. There are arguments for preferring python over php in some circumstances, but most of the reasons given in this article lack validity.

I never actually heard of FIG and have been using PHP for quite a few years. Are they new? I don't follow PHP news as close as I used to with having to use other languages more often so perhaps that is why. Only standards I was aware of for PHP are those that Zend[1] gives.

edit: Looking at the guidelines on FIG, they really should take some cues from PEP 8 and make the writing style look less hostile and less RFCish², if they want a majority of the community to willingly read it.

Discussing it with a fellow PHP developer looking over the FIG guides, he said it best (by quoting PEP 8):

"A Foolish Consistency is the Hobgoblin of Little Minds"

[1] http://framework.zend.com/manual/1.12/en/coding-standard.htm...

²Nothing against RFC and the format works for what they are trying to accomplish. It doesn't work so well for coding guidelines though.

FIG (http://www.php-fig.org/), of which Zend is a member, has been around for 2-3 years, but only really gained traction in the last 18 months or so.

No one cares about Zend coding standards anymore.

PHP-FIG is a committee of members hailing from a broad splash of frameworks. There has been very little leadership in the PHP community regarding coding standards (all frameworks have their own "coding guidelines" for contributing to core/building packages) so this group establishes standards for that.

They have branched out and there are efforts towards establishing common interfaces too (logger, http client, and so forth).

PSR-1 and PSR-2 are generally accepted as being authoritative on how to write "Good PHP".

The majority of the community[0] are following the FIG standards.

3rd party PHP libraries have become so much easier to read in recent years thanks in large part to the work of the FIG.

[0] - Majority of community being major players like most frameworks and almost all of the most popular libraries.

(comment deleted)
Good to know they're finally going for uniformity. I was mostly referring to the average developer whose code you might read at work or from some code one might find on Github.

Major frameworks and libraries going in that directly though is still a very good thing.

"When was the last time you looked at a PHP project and saw a list of all the third-party libraries and respective version numbers being used written in a single file?"

Huh? Any serious, modern PHP project uses Composer (http://getcomposer.org/).

given that it's < 2 years old and still labels itself an 'alpha', many (most?) projects might not be using it yet.
Incorrect.

Have a look at http://packagist.org

What's incorrect?

That it's labelled alpha by the developers?

https://github.com/composer/composer/releases

That it's barely 2 years old?

I'm not sure what pointing at packagist proves. There's a lot of packages published for it. Great. THE most popular package installed via packagist's counters is Doctrine, at 1.4 million installs. It's a lot, but compared to all the PHP installs out there, and all the PHP apps out there that aren't using composer (because it's alpha and not very old yet, my conjecture) dwarf the 1.4 million installs of a program via packagist. I'm probably 20 of those Doctrine installs myself.

I was referring to the statement "Any serious, modern PHP project uses Composer". There are loads of 'serious' PHP apps that don't use it, or may only be adopting it now. If the definition of 'modern' means "has to use composer", then, of course, "serious modern PHP projects" use composer.

I personally know people who aren't allowed to rely on it yet in their companies because it's still labelled 'alpha'.

Hey, did you switch to "Python 3" or "Python 2"?
2.7 originally. Django became a big focus of my early contracting days and support for 3 wasn't in yet.
And again... That article has nothing todo with PHP vs Python, its just about choosing the right framework and methodologies. All of the pro python arguments can be had for PHP as well if you dont work in a spaghetti codebase.
Didn't even bother with another dynamic language * , went straight to the JVM and discovered Scala (framework-wise, enjoying Play)

* not totally true, 6 months on Groovy, followed by 6 months of hell (AKA pre-2.0 Grails)

Can't speak for Python other than a couple of months of Django (out of the box CRUD functionality is indeed nice).

After a couple of years of Scala can't imagine going back to a runtime only language (other than Coffeescript/LESS via GruntJS for the front end)

I think everyone defending PHP, saying that it's as easy to develop well in it as in Python, is missing a valuable point: that owing to PHP's reputation as a "web dev intro language", there are far more inexperienced and incompetent PHP developers than Python developers.

See also: http://www.paulgraham.com/avg.html

So ? Bad/mediocre people shouldnt be hired then, no matter what language.
It's likely that there are also far more experienced and competent PHP developers than Python developers.

The PHP ecosystem is just vastly larger.

I think that people defending PHP are getting a point you (and OP) are missing: both PHP (the language) and its ecosystem matured a great deal in the last 2-3 years.

A lot of good ideas were either implemented into the language or ported over from other languages/frameworks. There are tools/frameworks that rival those in Python/Ruby land.

I know there is Symfony, Laravel, Slim and the rest but what bothers me about PHP is the language itself. It feels clumsy, full of monkeypatches all over it, as the result of many different shools of thought.

On the other side Python feels like one mind's labour, it just fits togheter.

"When was the last time you looked at a PHP project and saw a list of all the third-party libraries and respective version numbers being used written in a single file..."

Are you serious? This is what your hate boils down to: you don't know the PHP ecosystem well, at all. It seems like you've been living under a rock.

Going to your 8-4 every day and hacking away at your company's codebase and not bothering to read up on the goings-on about your language will result in bad times.

I am glad that OP has found the ideal language for him, but not knowing what PHP is well suited for after working on 12 years sounds absurd.

Every PHP dev after working on it for few years will know that its not the best language out there, but they will also realize that using it they can get stuff done.

Regarding books : I have to agree on the fact that there are many crappy books out there related to PHP, and I have still not found the ideal reference book for my needs .

Package Management : while its true that there was no standard package management tool for php few years ago ( except PECL ), the situation is different now, composer has evolved now and has become very common these days. composer does have the system where one can put all the dependencies in a single json file to manage all the dependencies .

Django's first tutorial : just go through Symfon2's first tutorial and in my honest opinion it can give serious competition to that of Django's http://symfony.com/doc/current/book/http_fundamentals.html

RESTful and DRY : OP mentiones some libraries which are good for creating RESTful API's in Python, and Of course PHP has some high quality libraries to create RESTful API's.

https://packagist.org/search/?q=REST

Evented Codebases : PHP does have some tools to do the achieve the same result http://reactphp.org/

Deployments : no one can deny the fact that PHP is knows for its ease of deployment ( shared hosting ) , but any ony one serious about their app cannot go with shared hosting , but deploying PHP on a VPS/dedi is actually easier then deploying a Django App . and even modern deployment tools can Chef, Puppet, Ansible have wide support for PHP and its frameworks.

I am wondering that how just by switching to a language will make my code RESTful, DRY and more maintainable. In the end even Python/Django can be used to write crappy code.

Actually I was expecting more solid reasons to switch from PHP to Python ( There might be some too ) .

Solution seems to be mostly about covering symptoms than addressing underlying issues.

"technical architecture of the code could change with a developer simply deciding to do things in a different way from the rest of the team" tells me there's no strong code review culture, or code discoverability, and you've simply moved the problem from a lower level to a higher one. While people might be adhering to Python idioms, they're probably not going to write the code that's idiomatic to the organization or make the best use of internal libraries.

I abandoned Java, C#, PHP, Python, and Ruby for F#, Lisp, Scala, & Haskell.
I'm currently learning Python from a PHP standpoint which is kinda exciting because Python is beautiful language and joy to work it. Do you know of any good Python books that cover 2.7? I know you mentioned Python Essential Reference I'll check that out as well most Python tutorials are outdated it seems.
I was for the longest time enamored with rails and the income potential of becoming a rails Dev...until I had finished a small ecommence app that I wanted to deploy on a cheap Linux host like hostgator or site5...both of which do support rails but only specific versions and getting everything to work was going to be a nightmare.

that's when I decided I needed to find a globally method to deploy which brought me back to php. Which initially pissed me off cause I was Loving Ruby's syntax.

then I discovered laravel..and holy shit I finally learned how to be a good php developer and use namespaces and really build some awesome stuff. It is rails on php and a hundred times easier to deploy on just about any lamp stack.

I dare say, it doesn't appear as if this person has been keeping abreast of the latest PHP trends. FIG standards go a long way and Composer is a godsend (prior to that I wanted to tear my hair out all the time).