PHP vs Python Part 1

Even though I started my web developer life with PHP, which at that moment I liked very much, once I layed my eyes on Python it was love at 1st sight. My nerdmones (nerd hormones) were all over the place. It was beautiful, just beautiful. The 1st thing I noticed was the syntax – plain, simple and gorgeous. The following image is a good analogy of the difference between the two:

php vs python

Besides all the lovely simplifications that enable you to write less code, Python contains no useless things like curly braces, dollar signs or semicolons. Let’s look at the following examples:

Python:

def fib(n):
    a, b = 0, 1
    while a < n:
        print a,
        a, b = b, a+b

PHP:

function fib($n) {
    list($a, $b) = array(0, 1);
    while ($a < $n) {
        print $a;
        list($a, $b) = array($a, $a + $b);
}

OR

function fib($n) {
    $a = 0;  b = 1;
    while ($a < $n) {
        print $a;
        $a = $b; $b += $a;
}

See how simple the Python code looks in comparison to the PHP code ?

  • “function” became “def”, which is shorter to write
  • In Python, blocks are determined by the indentation of the code, instead of the curly braces. No more chaotic indentations and useless symbols.
  • No more “$”. Isn’t it enough we’re screwed by money and bills every single day, must we see the dollar symbol in the editor as well ?!
  • Assigning values is easier
  • The “while” statement does not require opening and closing brakets (the same applies to the “if” and “for” statements)

Oh, and this is just the tip of the iceberg !

Not jumping with enthusiasm yet ? No problem. Stay tuned for more articles on PHP vs Python as I am sure that Python will eventually get to you !

9 Comments
  1. I think the whole issue of Python VS PHP is ridiculous. They’re both the same when it comes to the important stuff. And that’s why the whole discussion is moot. When you write OOP, both PHP and Python have major areas in which they’re lacking. The “dynamic” types, the annoying paths sorting, the ridiculous implementation of OOP keywords which basically mean nothing in both languages, and extremely difficult multi-developer environment.

    So Python is a little more structured. Its syntax is a little cleaner (and I do mean a little, because if you wrote PHP well then your code should look 98% the same as your Python code). PHP trumps Python as a web development language (fuck Django). When you write PHP using a normal IDE like Eclipse or Zend you will never notice having to write curly brackets and all those other “annoyances” (which exist in pretty much any programming language) because the IDE takes care of that as you type. PHP solutions are much more widely available, and numerous. They’ve been around for longer, and as far as open-source goes – that’s a big advantage.

    Bottom line – both languages are lacking where it counts – scalability. And I mean both code wise, and multi-developers wise. With the rise of affordable and scalable VPS hosting – I am considering going JAVA. If not for the comfort, then at least for ending this stupid debate about PHP vs Python.

  2. 1st of all, this is mostly my subjective opinion, sprayed with some objective arguments.

    Some may like PHP, some may like Python.

    Yes, they can both be used to create a web site. Most clients don’t really care what’s driving it. But for me it’s those small (maybe not that small) differences that count. I just love Python. And not just because it has no curly braces, dollar signs or semicolons, but because of lots of other great stuff like string & array manipulation, generators etc. Those are very nice things to have. But I will get to them in some future articles.

    Maybe there is not such a big difference between the syntax of the two, but like I said, those little differences really count for me. Why overcomplicate things when you can have them so clean and nice ? It’s pretty useless…

    Oh and.. with Python’s syntax you won’t need to worry about coding standards that much as it’s pretty strict.

    Yes, PHP is more used for web development than Python, but only because it’s a bit more accessible to newbs, it’s easier to deploy and had/has support from some big companies. And I think it’s more of a trend to use PHP. It’s like one of those fashion things.

    Oh.. as for JAVA… well… I hate it ! You need to write so much code to do even the simplest things. As for the other aspects of JAVA, I really don’t know, nor do I care about at the moment.

  3. I agree, Java sucks!

  4. I like python, but sticking with PHP for most web work.

  5. Unfortunately, Python solutions are pretty expensive at the moment compared to PHP solutions – another argument on why PHP is more popular.

  6. Marian, your points are still Python VS PHP. Java and other programming language, as opposed to script languages like Python and PHP protect you, the programmer, from major mistakes. And in the long run, you write less code, not more.

    If I had to do a tiny website I would use PHP – it’s relatively lightweight, numerous solutions are available and I can get something up in less than an hour using either 100% custom code or one of the many frameworks and CMS software that exist for it. Apache + PHP is a very straightforward installation that takes a couple of minutes at most.

    However, when I’m writing big stuff, like I do now for http://goBeepit.com – this is a multi-layered project with many facets and components. From a multi-faceted database, RESTful API for the Android & iPhone app, numerous web apps, and all the way to a simple website. PHP (and Python) start to break. It’s very difficult to bring another developer on-board without messing shit up big time. And that’s mostly to do with the not-fully-functional OOP implementation in both languages. In any language, it’s up to the developer to structure their code correctly. However, Java and other fully-realised programming languages don’t make you write more code. They simply guard you from mistakes that could (and would) take hours to debug.

    When writing OOP, PHP and Python make you write more code because of their lacking implementations. It’s as simple as that. PHP was born out of a necessity back in the day. Python came out of comfort. But now, with affordable VPS services with full root access – it seems like the role of both languages is small when it comes to the web. BTW, I believe Python will outlast PHP because of its other uses, like small mods to games and software. In that regard – PHP is useless compared with Python. But both should die as web languages.

    To sum up my argument (as frazzled as it is) – If I had to do something small for the web at this point in time I’d choose PHP because it’s the easiest and when I write my code, in a normal development environment – it’s clean, tidy, readable, and you will not notice the curly brackets etc. Maybe if Python grows in popularity and starts offering a web building solution better than Django, I’ll start giving it more thought. If I had to write something big, like I’m doing now. I would not choose PHP or Python. Both of these languages have the same faults when it comes to scaling the project and it’s simply counter-productive to use them.

    P.S.
    If you like Python so much, get an account @Rackspace. They’re dirt cheap and you get a magnificent service. Just install Python on your VPS via SSH and get going man. These days it’s easy and affordable to write in whatever language you prefer. It would cost you around £6 per month to get a 256mb machine. I am hosting around 12 websites on their 256mb machine and it’s actually an overkill. If you need more, it’s also cheap to upgrade, and super fast and easy without the need to re-install anything. Choose Rackspace.co.uk to enjoy their London Data Centre and avoid any future ridiculous US laws.

  7. I don’t want to transform this post into PHP vs Python vs JAVA as I used JAVA just a bit, so I’m not really in the position to make this kind of comparison.

    For tiny sites, I’d use PHP as well, most probably WordPress.

    For larger projects, Python + Django are way better !

    It depends on the nature of the project, I guess. Maybe Python ain’t enough for some projects, but you can always mix it with something else.

    Anyway… I won’t argue with you about JAVA. YET ! :D Who knows, maybe in the future I will have to learn it as well.

    Btw… maybe you’d like to comment on why you dislike Django so much on this post: Python and Django Part 1.

    And thank you for your recommendation ! :)

  8. You havent even touched the topic of speed of PHP vs speed of Python.For a web developer, PHP is the first thing to go to after a person goes through basic things needed to get a hang on starting actual backend programming ( such as HTML, CSS, JS or jQuery ), because if a user was messing around with JavaScript before starting to learn PHP he would have less sharp of a learning curve on it, than starting to learn Python for example since the code structure ( with curly brackets, similar function definition etc ) is more like PHP than like Python.That is very important for a newbie programmer wannabe, than if he is going to have to write a curly bracket here and there or not. If a user thinks – “meh, this looks so much unfamiliar for me, will take ages to learn completly different semantics”, he will quickly give up on learning a language. That is why I believe PHP is a first stop – and should be a first stop for a user that wants to dive in web development. Once he becaome intermediate – advanced in PHP, he should get a grasp on Python language structure and start learning it if he wants to in order to maybe swap it in instead of his previously used PHP for development. But even so, I don’t think anyone would want to learn a completely different language to do the things he is already doing without need to learn more things, since PHP on its own is developing from version to version, there are so many frameworks for it to learn so a user wont be able to completely “master” it in like 2-4 years anyways with all that material available.Only enthusiast would start learning more languages he needs to in order to make a living – I am one of them though and currently learning a bit of Python while I am working on my daily job(s) as a web developer and web support

  9. Hehe, yes, I agree… I barely scratched the surface.

    But this blog post was more of my subjective view over the matter than an objective comparison, anyway.

    I, as well, think that PHP is a good 1st choice when entering the web development field. It’s pretty easy to learn, with tons of resources over the internet and more importantly, it’s the most popular programming language when it comes to web development.

    So yes, a beginner has higher chances to find a job, by knowing PHP. This was also my 1st choice and am still working with it.

    But, I for one, learned Python pretty fast. It was love at 1st sight, so I guess that was mainly the reason.

    I wish it were more used for web development… Python and Django make such a great team.

Leave a Reply