php-python
 Rating: 1 Star2 Stars3 Stars4 Stars5 Stars
Loading ... Loading ...

PHP vs Python: Analysis



PHP vs Python – What does it take to state one language better than other? One answer can be flexibility, development friendly, licensing policy (open source or commercial), community,  portability, dynamic typing, support for variable number of function arguments and ability to freeze live objects in a string representation. Documentation of course is a major player when you choose a language because you still have to sharpen your skill and you haven’t worked on that particular language yet.

Features which supports PHP

  • syntax very close to C and Perl, with curly braces and dollar signs
  • the ‘switch’ statement and ‘do … while’ construct just like C
  • increment and decrement and assignment operators
  • the ternary operator/statement (… ? … : …)
  • schizophrenic tableau of function names. There are no namespaces, so functions often have prefixes to denote their source (but often not). Functions are often placed into classes to simulate namespaces.
  • a very casual language, where globals are often used to pass arguments (global variables should not be used, that is language independent)
  • commonly installed environment
  • aliases (‘$a =& $b’ means that when $b changes, $a changes also) (should be “references”, not “aliases”, but are called aliases)
  • one array type that doubles as a list and a dictionary. Dictionary keys are iterated in their original order.
  • Excellent Documentation
  • Huge Community base with huge supportive codebase available online

Features which supports Python

  • namespaces and modules
  • small core
  • indentation to mark out block structure rather than curly braces, which make code look prettier
  • clear, concise, and orthogonal syntax
  • self documenting with docstrings and pydoc (PHP 5 has reflection and doc strings)
  • keyword arguments to functions and methods, easy support for default arguments
  • true object orientation and ‘first class’ classes and functions
  • classes are used extensively in the standard library
  • multiple inheritance
  • object-oriented file handling
  • method chaining
  • everything is a reference
  • ‘del’ statement for all data types
  • consistent case sensitivity (PHP does for variables, but not functions) (Functions are case insensitive)
  • simple array slicing syntax
  • iterators (PHP 5)
  • structured exception handling (PHP 5)
  • operator overloading
  • threading
  • lots of high-level data types (lists, tuples, dicts, mx.DateTimes, NumPy arrays, etc.)
  • dates that aren’t limited to UNIX timestamps (<1970, >2038)
  • support for all major GUI frameworks
  • strong internationalization and UNICODE support
  • maturity, stability and upward-compatibility
  • tends to lead to much more scalable applications

Unlike PHP, which has web development features built directly into the core language, Python’s web development capabilites are provided by add-on modules. Basic CGI capabilities are provided by the ‘cgi’ module which comes in Python’s standard library. There’s also a wide range of third-party modules available for Python; some are complementary, others compete. As a result, Python provides a more flexible base for web development.

There are some adverse side effects of this flexibility. First, the range of choices can be bewildering. Unless you are working experienced Python web developers, PHP is easier to get started with. Second, support for PHP is more common with shared-hosting companies than support for the various Python options.

Another difference is that PHP is embedded in the web server, whereas Python web applications can either be embedded in the web server like PHP or run in a separate process.

Now, lets test how fast they execute to find all the prime numbers under 10000. We will execute the test three times by optimizing the outcome

$ time ./script.php
Language Script 1 Script 2 Script 3
PHP 1.359 1.753 1.191
Python 1.894 1.636 1.634

Well, this shows that PHP runs faster than Python but here is a catch. PHP can run faster for smaller codes but when we talk in terms of scalable large system then Python will perform better. The above code was small where we were finding all prime numbers under 10000 and PHP shows why its choosen in most of the small web applications. No doubts there are few big names with PHP backing too.

Lets do one more analysis where mathematical calculations are performed

Sample script loops through a FOR statement 2,000,000 times calculating the MD5 hash of N + N, N equaling the number of passes thus far.

Results:

Round 1 – 2,000,000 Passes
PHP = 21.4227 sec
Python = 9.8737 sec

Round 2 – 2,000,000 Passes
PHP = 21.1122 sec
Python = 9.7241 sec

Round 3 – 1,000,000 Passes
PHP = 9.811 sec
Python = 4.429 sec

Round 4 – 1,000,000 Passes
PHP = 9.857 sec
Python = 4.280 sec

As you can see, Python is more than 2 times faster than PHP in performing this operation, which is more towards executing a mathematical algorithm.

Another Opinion – Python has been optimized for mathematical algorithms so in that respect it will blow PHP out of the water but if you compare which language can server more web pages in a period of time you find that PHP is noticeably faster.

Other important Aspects which make a language preferable are listed below:

Speed of Execution

Given a fairly intense problem neither language has a large advantage over the other, and each will be better for different things. We can thus rule out execution speed for 99% of what we do.

Speed of Writing

Assuming the languages in question all run at an acceptable speed (and they probably do) this becomes the second most important metric (number 1 being if they will actually run). I must be honest here and say that I have not used Python long enough to be sure how fast or slow I am for developing in it but I can say that so far it seems faster.

Maintenance

I may have not been using Python long but I can hands down say that it’s easier to maintain. I indent stuff in PHP anyway but the lack of curly braces is really nice and I prefer the scope access of Python over PHP (object.property vs $object->property) but I think that’s a matter of personal opinion that you need to decide on for yourself.

Ease of Setup/Portability

PHP wins here, you don’t need write your own HTTP headers as you do with Python, more hosts support it, it’s easier to connect it to a database and there seem to be more tutorials on it.

Support/Community

I would say PHP community and Support is better but with days passing by i have noticed large Python community helping out.

Other Sources:

I liked may comparisons but few are very good i am listing the ones below, i will keep on adding when i get time.

Summary

I would prefer Python for the sake of developing a scalable application (its not like you can’t do in PHP), but its easier in Python. Moreover, Google supports Python with its Google App Engine where web sites can be hosted on Google’s server for free, it also supports Python frameworks like Django so my bet will be on Python. PHP on the other hand still have 60% of the market share and i bet with the new release (6.0) in the corner they will certainly fill up the gap by including namespaces,high level data types etc.

If you’ve got any thoughts, comments or suggestions for things we could add, leave a comment! Also please Subscribe to our RSS for latest tips, tricks and examples on cutting edge stuff.

Related Posts

php-domain-information

How To Get Domain, Subdomain, TLD, CTLD & Path from URL In PHP

braintree-payment-gateway-integration-php

Integrating BrainTree Payment Gateway With PHP

How to create XML with PHP Mysql

How To Create XML With PHP MySQL

RBS WorldPay Integration With PHP

Integrating RBS WorldPay XML Direct Method with PHP

  • Pingback: upnews.it

  • Pingback: Blogsvine

  • SomeDude

    Hi,

    once again somebody, in this case, you, has mistaken performance with scalability.
    Those are not the same,
    An app with bad performance can still be very scalable
    An app that can't be scaled can still have a very good performance (until too many people use it :P)

    Of course, if the app is performant you won't have to scale out as early but still…

    This is such a common mistake committed on many blogs, it makes me cringe everytime.

  • Isaac Gouy
  • MikeRT

    One of the key advantages that Python has over PHP is the fact that it is a language with a specification and multiple implementations. CPython, the standard Python release from Python.org, may have no speed advantage over PHP, but I would hazard to guess that IronPython running in an ASP.NET environment would beat PHP hands down on performance. Same thing with Jython if you run it through jythonc and load it into Tomcat or Weblogic.

  • http://www.hurricanesoftwares.com Ashish

    I wasn't aware of IronPython on ASP.Net environment. Thanks for bringing that to my notice. I will definitely test that out.

  • Pingback: Hurricane Software: PHP vs Python: Analysis : WebNetiques

  • Pingback: Hurricane Software: PHP vs Python: Analysis : Dragonfly Networks

  • MikeRT

    You can also download Jython and use it to build Servlets and Portlets. There might even be experimental JSP support for it now. To test it out as a Servlet, make sure that you use the jython compiler to build it into Java classes and that you put the Jython dependencies into Tomcat's shared library directory.

  • kelvin

    “Another difference is that PHP is embedded in the web server, whereas Python web applications can either be embedded in the web server like PHP or run in a separate process.”

    This is wrong, PHP can run as embedded, CGI or seperate process.

  • Marek

    One important fact: Python reads source code for the first time, parses it, optimizes and then store in to the file the byte code for next executions, whereas PHP parses it always, unless you've installed eAccelerator, APC or another extension. I bet you've benchmarked plain PHP, but then you're comparing PHP's read, parse, optimize, execute versus Python's just execute, which is clearly incomparable!!
    Please, try to install one of them, repeat tests and submit them again, just to be fair ;-)

    And several other hints:
    - PHP namespaces are coming already to version 5.3
    - upward compatibility you mention as Python's highlight is myth; read Python 3 aka 3000 planned changes and you'll learn there is planned to break many existing APIs

  • Rico

    I find this article and articles like this a complete waste of time!

    PHP vs. Python!? Who cares!?

    Use the tools and programming language that gets the job done the fastest, easiest and most clean way.

  • margesh

    Rico it depends on how you manage and plan your application. If you need a scalable application then you would definitely try to opt for best language.

    Choosing language to get the job done isn't that easy and you need to be aware of every aspects of supported features of the programming language .

    I think you are simply coding in PHP or Python at very beginner level because only beginner can say words like you said above.

  • EllisGL

    Corrections for PHP:
    1.) I've never heard the term “aliases” used when talking about references.
    2.) PHP can do method chaining.
    3.) Easy multidimensional associative arrays

    I do believe that PHP need a good reworking thou to get around the too many chefs cooking a can of chicken noodle soup syndrome.

  • Pingback: PHP vs Python — Mana yang lebih baik? -- Review Teknologi Web dan Web Programming

  • http://phpblogger.de/ harald

    application / programming language level is not the only thing to consider, when thinking about scalability. there are much more important factors — imo — like: which environment you are running in, caching strategies, etc.

    a python application does not scale better, cause it's python, but maybe someone considered better strategies to build the application.

  • http://groups.google.com/group/phpvietnam pcdinh

    Very funny performance comparison. md5 function is not part of a language. It is just an security-related function. Just like sha1() or hmac() or sha256(). It can not speak for a certain language's performance. If md5 performs poorly in PHP, you can say that md5 algorithm implementation in PHP is not good.

    You can compare language implementation performance by comparing a set of large enough and most used functions and language constructs.

    So what do you mean by “tends to lead to much more scalable applications”? Any case study?

  • http://www.hurricanesoftwares.com Ashish

    Md5 implementation in PHP is fairly easy and nice but not as secure as of Python. Though, it may sound debatable. Generally programmers don't double check the security issues. Most security issues arise from programmers making assumptions when they shouldn’t.

    Python applications are rather complex and usually involve a lot better programmers who take care of the security issues. I am not saying that Python is more scalable than PHP but if you look at the big applications and new app's coming out you will notice how Python has delivered much scalable applications than PHP. Many of the Google applications are in Python. Why do you think they have chosen Python?

  • http://www.bartonseo.com Barton

    Long live Python!

  • Dan

    Hi!,

    let say my 5 cents…

    -PHP has too crappy embedding system. I guess its need for Zend.com… PHP seems as not free for this reason.

    -Python has poorly support for static variables, methods. This is not suitable.

    -Pythons indentations is a hidden jail

    -Not good bytecode support in PHP. APC and others cant store cached code as separated “static file”.

    -Pythons has many bicycles for runing in web servers.

    -Require_once, Include_once, autoload is hell in PHP

    -Too late and asswide namespace support in PHP

    -No _private and _protected vars, methods in Python

  • http://www.codeassembly.com codeassembly

    “Sample script loops through a FOR statement 2,000,000 times calculating the MD5 hash of N + N, N equaling the number of passes thus far.”

    If you are calling the md5 function then this is irrelevant , because the function is written in C you're benchmarking the loop not the md5 algorithm implementation, both languages are glue, they call module functions written usually in C, most scripts written in these languages will be a bunch of such calls with some little glue code to hold them together.

    Neither php or python is used to write complex algorithms or data structures (imagine writing something like the md5 algorithm in php or python, they are extremly slow for intensive processing algorithms).

    So performance lies in your algorithm implementation and in the algorithms built in the C (mostly) core of the language, parsing and evaluating code is just a very small percent of the overall performance, from this point of view (performance) they are the mostly the same.

  • http://groups.google.com/group/phpvietnam pcdinh

    > Md5 implementation in PHP is fairly easy and nice but not as secure as of Python
    Do you really know what you are saying? Have you ever implemented a mathematical algorithm?

    > Python has delivered much scalable applications than PHP
    I dare to say that you even do not have any definition of “scalable” in your mind at the moment. You think that I don't have any experience with Plone, which is a deadly slow application?

    > Many of the Google applications are in Python.
    > Why do you think they have chosen Python?

    Do you know that PHP powers lot of extremely high traffic websites in the world? Wikipedia, Facebook, Yahoo, Flickr, Rapidshare ….. Why do they build them in PHP, not Python? Which ones are in Python? Google? Gmail, Google Calendar, Google Earth are made in Java and C++. Lot of Python code used in Google is for batch proccessing

    > Python applications are rather complex and
    > usually involve a lot better programmers who take care of the security issues.
    Why are they complex? Because of complexity nature of Python language or project requirements? Why are they developed with so-called “lot better programmers”? Who are “lot better programmers”? Where do they come from? Why do they work on Python projects instead of PHP, Java, C#? You need to explain all the things before you conclude.

    You are making assumptions that Python is apparently better than PHP without any evidence or even knowledge

  • mchrisneglia

    I rewrote a dead simple script for spidering a large number of websites. with php, i used curl, with python I used httplib. It was my first python script, so take this with a grain of salt. Note also that the python script wasn't as sophisticated (ie, with curl you can make concurrent connections and tell it to follow redirects but in python you have to do this yourself with a recursive function or something similar, or use the twisted / some other framework). discounting those differences, I found that python was orders of magnitude easier, briefer to code, and got the job done faster than curl working concurrently (on windows/apache2, mind you) because curl and php has weird issues, even when running php as fastcgi

    now the moral of this story is, that when you try to do anything interesting in php it craps out because of the quality of the libraries you are using. It is too easy to code something and then read somewhere that your app that uses a function like 'array_key_exists' is really slow and better to use isset. It's too many things put together into a franken-mess, the community is great but it's like herding cats. From what I've read and from my initial impresssion-and I'm too much of a noob to say with authority-that python's libraries are just better quality and more performant. Some of this comes from the principle of the language for there to be only one good way to do something.

    Personally, I love php and will probably still continue to use it because I work with drupal and codeigniter, no way aroudn that. But I probably will be transitioning to python. Pydocs can't compete with php.net though, and that's probably why it hasn't taken over market share. If you could have web programmers who get onboard with python, they could leverage their skills to make real desktop apps without having to do something stupid prism, adobe air, google gears type browser-development enviroments.

  • mchrisneglia

    I rewrote a dead simple script for spidering a large number of websites. with php, i used curl, with python I used httplib. It was my first python script, so take this with a grain of salt. Note also that the python script wasn't as sophisticated (ie, with curl you can make concurrent connections and tell it to follow redirects but in python you have to do this yourself with a recursive function or something similar, or use the twisted / some other framework). discounting those differences, I found that python was orders of magnitude easier, briefer to code, and got the job done faster than curl working concurrently (on windows/apache2, mind you) because curl and php has weird issues, even when running php as fastcgi

    now the moral of this story is, that when you try to do anything interesting in php it craps out because of the quality of the libraries you are using. It is too easy to code something and then read somewhere that your app that uses a function like 'array_key_exists' is really slow and better to use isset. It's too many things put together into a franken-mess, the community is great but it's like herding cats. From what I've read and from my initial impresssion-and I'm too much of a noob to say with authority-that python's libraries are just better quality and more performant. Some of this comes from the principle of the language for there to be only one good way to do something.

    Personally, I love php and will probably still continue to use it because I work with drupal and codeigniter, no way aroudn that. But I probably will be transitioning to python. Pydocs can't compete with php.net though, and that's probably why it hasn't taken over market share. If you could have web programmers who get onboard with python, they could leverage their skills to make real desktop apps without having to do something stupid prism, adobe air, google gears type browser-development enviroments.

  • http://www.contussupport.com Php Programmer

    PHP is really no match to Python. I have been PHP developer for 3 years and switched to python because i feel Python is more like a hybrid language. Because of python flexibility features i can easily scale.

  • Pingback: PHP 碰上 Python | 網頁開發網誌

  • http://www.vemployee.com/hire-php-programmers.html PHP Programmers

    PHP is more famous because of its frameworks

  • Pingback: 老頭的爬文查看 python 的第一天 | 林中路