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.