Python VS PHP

Python VS PHP

Aah, the old programming language debate, much akin to comparing religions or political parties – bound to end up in a fist fight. Some will argue that as long as you choose the right tool for the job, it doesn’t matter, but I’ve found it’s always the little things that break the camel’s (or in this case, the programmer’s) back.

I wrote a little Python pet project (mind the pun) this week after years of web development using only PHP. As a matter of fact, my first job was a Python position. Eventually, as I started doing GUI development in Delphi, Python was relegated to odd jobs and hacks. When I started doing PHP development, Python became redundant and eventually my skills became rusty as old nails. Until now.

Even after just one week using Python, I remember why I’ll always prefer it – it empowers me as a programmer. In the Python community, they refer to it as the “batteries included” philosophy – with the mere inclusion of a library, anything becomes possible. Combine that with clean and readable syntax, fluent interfaces, powerful list comprehensions, list slicing, an interactive console, and too many other features to mention, it’s clear to see why.

XKCD

I’ll illustrate using a Python example:

date = details[1].split("<td>")[1].split("</td>")[0].strip()

This line takes the second element of a list (the Python equivalent of an array) and parses a date string out of an HTML table cell. Whether or not it is advisable to do this is a topic for another day, but it illustrates quite handily how the combination of “everything is an object”, great list operators and string functions makes a hack like this effortless. Here is the equivalent PHP code:

$parts = explode('<td>', $details[1]);
$otherParts = explode('</td>', $parts[1]);
$date = trim($otherParts[0]);

If you’re a PHP developer and haven’t worked with Python before, do yourself a big favour and head over to the Python tutorial or check out any of the multitude of hacks, projects and frameworks written in Python and see how it’s the little things that make programming enjoyable.

Technologically Impaired Duck

Categories: Development

  • Heidi

    Is this how you got your black eye Niel?

  • http://twitter.com/paulscott56 Paul Scott

    I always encourage PHP devs to learn at least rudimentary Python. Not so much a Python *web* framework, but a little at least on the scripting side. I see PHP code improves significantly with respect to readability and sometimes efficiency as well.

    In the PHP work that I do, I stick to strict OO PHP, I even make use of an Object object, which emulates the Python paradigm quite efficiently. That way, I get to have the best of both worlds in a matter of speaking.

    As with anything though, there is no silver bullet/holy grail/whatever. Intelligence in choice mainly comes from experience. Anything can be done well, or not so well, language agnostic!

  • Att

     You can this also so simple in PHP wit object orien way, php isnt just procedural language;)

  • ryeguy

    The majority of the built-in functionality in PHP is a random, incoherent, procedural mess. PHP has OO capability, but it is not an OO language.

  • Hermanradtke

    Did one really take more effort than the other? I love python, but I have to say that the php example is more readable and easier to debug. I doubt you saved too much time chaining everything together in python. This reminds me of how some people write javascript. Variables are not expensive. Use them.

  • Neil Broers

    What really gets me with PHP is that you can’t do array dereferencing. In the example above I could rewrite the following:

    $otherParts = explode(”, $parts[1]);
    $date = trim($otherParts[0]);

    To:

    $date =  explode(”, $parts[1])[0]

    Well at least not in 5.3. Maybe someone can confirm if this is possible with 5.4 alpha1?

    See http://schlueters.de/blog/archives/138-Features-in-PHP-trunk-Array-dereferencing.html

  • Neil Broers

    Well, what do you know, it has been included in 5.4 alpha1:

    Added: Array dereferencing support

  • http://twitter.com/ShoesButtback Shoes Buttback

    Method chaining is not the same thing as a fluent interface.

  • Petah

    I see your import, and I raise you a require_once

    require_once ‘simple_html_dom.php’;
    $date = trim(str_get_html($details[1])->find(‘td’, 1));

    Right tool for the right job. I would hate for your hack to fail if the had an attribute in it.

  • Roger Llopart Pla

    What is the “random, icoherent, procedural mess” are mostly functions for primitive types in PHP (array, string, integer, etc). Resources are slowly (to much slowly IMHO, but I ain’t a PHP Core Developer, so I can’t complain) being changed to objects (Spl classes, PDO).

    It’s built with a procedural language as a core to give a lower entry level. But after entering, It’s quite easy to swap to OO mentality.

  • Anonymous

    Your article is total flame bait and devoid of any actual content. You share some opinions but back up very little in terms of actual, useful comparison. For those of us who are language agnostic I don’t think you are adding anything of value.

  • Jörg

    Since split sounds like regexp, I call you with:

    $date = preg_replace(“/(.*([^<]*).*/mUse”,”trim(‘\1′)”,$details[1]);

  • Jörg

    missed the opening tag in the regexp obviously. But the idea is conveyed.

  • Guest

    OT but SimpleHtmlDom is a crap library. Slow. Doesnt use libxml. Horrible Codebase. Use DOM or phpQuery or fDomDocument or QueryPath or even Zend_Dom to name a few alternatives.

  • http://srirangan.net Srirangan

    Although I do prefer Python, that example is horrible!

  • Anonymous

    Both have their pros and cons. Python is handy and fluent, PHP is improving, array deferencing is around the corner. In python I miss method visibility.

  • http://www.thecoderush.com Stjepano

    Yes python empowers you to do this:

    date = details[1].split(“”)[1].split(“”)[0].strip()

    But nobody should write code like that, it’s unreadable!!

  • http://doh.ms Rafael Dohms

    Just one correction: It was built as a procedural language because that’s what the creator needed at the time, it was never designed to “be famous” it actually gained more ground after it gained its OO aspects.
    But yes, it is quite easy to lear and swap to OO.

  • http://www.cnicodeme.com Cyril Nicodème

    I’m not sure comparing the language just by how many line of code you wrote is relevant.

    Just ask jQuery devs :p

    Don’t get me wrong, I like working with those three (Python, jQuery and PHP), but I was expecting something more deep than just who have the biggest.

    Maybe performance comparison, usage comparison (PHP is more used for web dev because it is installed “by default” on almost any hosting company. It’s not the case for Python), learning curve, community, etc.

    By the way, PHP is more known for web dev for a simple reason. At first, and also still on many website so far, website written in PHP doesn’t necessarily use Rewrite rules, so you often find links like site.com/page.php.

    For other languages (Python, Ruby, etc (using their web frameworks)), rewrite rules is included by default, that mean, no .py/.pyc files (oh my god that would be awful).

blog comments powered by Disqus