The real difference between PHP and Python

Tweet

Posting in the full realization of the futility of doing so, there’s some PHP bashing (as usual) happening on reddit at the moment: PHP vs Python – the real difference, brought on by this mildly amusing image. While I can accept the points – technically it’s actually much harder in handle errors uniformly in PHP and the community is less rich in computer scientists than Python – the corresponding flame war on reddit manages to miss a different point, which is easiest expressed in code.

What’s the most significant difference between these two scripts?


<?php
$hits = 0;
printf ("Hits: %sn", $hits);
$hits++;

And a web.py controller (absolutely no criticism intended – picked it because I like it – discussion applies to pretty much anything non-CGI and, in fact, this is really nothing specific to Python either)…


#!/usr/bin/env python
import web
      
urls = ( '/.*', 'counter' )
hits = 0

class counter:        
    def GET(self):
      global hits
      print "hits %s" % hits
      hits += 1

if __name__ == "__main__": web.run(urls, globals())

Depending on how you deploy the latter (i.e. not as a CGI), it will actually count – i.e. state can persist between requests. While that enables this feature, it’s also a potential source for very-painful-shooting-of-self-in-foot. There’s heavy feeling in the gut when you start wondering whether weak references might help fix memory leaks somewhere in your large code base. That curry may have tasted good but wait until the morning after… Remember those JScript memory leaks – do you really want the same fun server-side?

“But yes of course Sir. Of course you’re smarter than that. No, one singleton can’t hurt can it? You’d never make that silly mistake. No never. Not even on your dullest of days” ;)

How does this relate to errors and this image? Because PHP “resets” after each request ( see here or for much more detail here ) it’s actually not always necessary to handle errors explicitly – assuming there’s not something fundamentally “broke” about your code and it’s some kind of runtime error (e.g. db is down), it’s often enough to just ignore the problem and wait for the system to “right itself” – nothing is going to leave PHP in a state it can’t recover from. It’s a rationale that Damien Katz does so much better in Error codes or Exceptions? Why is Reliable Software so Hard? – read it – read it all.

Anyway – apologies to Python / web.py again – just following the reddit thread – could equally have been Rails, Catalyst or .

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • http://users.cg.yu/divine Divine

    I always have problem in deciding on what side are you on — based on your blogs. ;)

  • http://sixtones.com.au LinhGB

    Maybe Python web-programmers should just use their time to code more Python web-apps instead of wasting it on criticizing other languages. It reminds me of a saying “There are two kinds of programming languages: the one that people complain about and the one nobody uses.” Of course many people use Python but I can’t even think of a popular Python web-app (apart from Django the framework – but that’s not an app) off the top of my head.

  • Fenrir2

    Rails resets state like PHP.

  • Masklinn

    Since when are strawmen counted as valid arguments? Why would anyone in their right mind create a global variable here, even more so since it’s in no way equivalent to the PHP script above? (in the PHP script the counter is local to the request, in the Python script you created it global to the dispatcher).

    Oh, by the way, the part on functional programming that you link to is not very good (Erlang is more “scripty” than other FP languages because it has a shell? WTF, I don’t know of ONE FP language that doesn’t have a shell, even Haskell has one, and Lisp shells are a hundred times more powerful than the Erlang console which is really used for administration more than anything else). But he’s at least right in pointing out that error handling in Erlang is extremely interesting, I do support his suggestion that people look it up.

  • Reading comprehension can be soooooo hard huh?

    Masklinn: “Why would anyone in their right mind create a global variable here, even more so since it’s in no way equivalent to the PHP script above?”

    Harryf: “What’s the most significant difference between these two scripts?
    ….
    Depending on how you deploy the latter (i.e. not as a CGI), it will actually count—i.e. state can persist between requests.”

    Masklinn: “Erlang is more “scripty” than other FP languages because it has a shell?”

    Article: “Erlang is different in that it’s far more dynamic and “scripty” than other functional languages (it even has an interactive command line mode), making the development process more incremental and approachable.”

  • malikyte

    Divine: Harry’s on the side of the spectrum that believes one should use the right tool for the right job…and to understand what the strengths and weaknesses of those tools are so that he doesn’t end up “hurting himself or others” while wielding them. :)

    (Was that a good metaphor?)

  • http://www.saumendra.com Saumendra Swain

    Weather its the persistance of data from one script to other or the implementation in CGI environment, the basic need and the nessesity right now is to develop systems which are easy to handle and easy to manage. Hoping from web 1.0 to web 2.0 and then to web.3.0 requires the simplicity and mass recognition. And In all these factors PHP is leading the path.

  • http://blog.heuristicdesign.co.uk Hal9k

    Python is a very elegant language and has had proper OO support for a relatively long time. If I could I’d use it over PHP. However Python is harder to install and get running on a server; if you’re on a cheap shared host it’s not feasible. WordPress etc. have shown how big the cheap shared host market is. However if you’ve got the luxury of a root prompt, then I wholeheartedly recommend it.

    Another big factor is that Python is supposedly a “batteries included” language, which to most part is true, but currently it ships with very little database support.

  • SimonG

    LinhGB: There are a number of good python webapps in common use. Many open-source projects use Trac for example. Reddit itself is written in python (after moving from Lisp).

    Not all python fans just trash other languages (in my experience Ruby users are *worse* here), but spend their time doing work.

    –Simon

  • http://www.devmag.net devmag

    Hey!
    Interesting post. I would actually appreciate if you could write or publish here an article about the ZOPE Framework. It is an awesome application- and webserver. There are many CMS solutions that are quite easily adaptable! For example zms-publishing

  • http://mgaps.highsidecafe.com BDKR

    I’ve used Ruby and Python both in addition to PHP. I really think that these kinds of discussions get stupid after a while. The way those guys are acting over at reddit (most of them at least) really shows a lack of maturity.

    If anybody can’t see the plus side of any of the “other” technologies, they are more likely fanatical kool-aid drinkers then real and level headed developers.

  • http://www.pixelsoul.net pixelsoul

    I don’t know much about pythong umm python :p but pythong these days is pretty much the standard in 3d apps, so i suppose it would useful if you intend to develop for more then webapps.

  • http://maetl.coretxt.net.nz/ maetl

    BDKR: you’re totally right. That reddit thread is full of male geek bullshit. With this kind of crap so prevalent, it’s no wonder that the software industry has a problem attracting female developers and compsci enrollements at universities are falling…

    All programming languages suck. Sure, Python and Ruby are better than most, but they still suck in certain ways. If PHP makes you sick to the stomach, it’s a sign you shouldn’t be using it. But you have to wonder about people who supposedly don’t like PHP, yet are so obsessed with how bad it is they must spew forth countless anon posts ranting about it.

    SimonG: The Python community has traditionally had more issues with agressive flaming and infighting than most other language communities – it’s a much more recent thing with Ruby, largely due to an influx of Rails fanboys.

  • http://www.igeek.info asp_funda

    @LinhGB:

    Of course many people use Python but I can’t even think of a popular Python web-app (apart from Django the framework—but that’s not an app) off the top of my head.

    Mailman? I think that popular mailing list software is written in Python, no! ;) But yeah, I agree, people should use their time creatively instead of criticising uselessly!!

  • Anonymous

    Of course many people use Python but I can’t even think of a popular Python web-app (apart from Django the framework—but that’s not an app) off the top of my head.

    how about youtube and google ?

  • web tasarım
  • Sithembewena Lloyd Dube

    popular python web apps? yelp.com, slide.com(lots of python there), bittorrent.com are some of them. being an intermediate web dev who was raised on strict OO (ASP.NET), i feel more at ease with both, i.e. Python+Webware+Cheetah and/or RoR (most PHP code i’ve seen looked far too cryptic for the job at hand). i’ve also seen Smarty, which could change this if used properly.

    thats just my 2 pennies’ worth, and every developer should use the right tool for each job. i dont use my personal preferences as an excuse to be a troll.