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
- 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
|Language||Script 1||Script 2||Script 3|
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.
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.
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.
I would say PHP community and Support is better but with days passing by i have noticed large Python community helping out.
I liked may comparisons but few are very good i am listing the ones below, i will keep on adding when i get time.
- Pyjamas (Python GWT) – Introduction and Creating Widgets
- Performance Comparison – C++ / Java / Python / Ruby/ Jython / JRuby / Groovy
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.