Wednesday, October 29, 2008

Python vs. PHP

Python vs. PHP

Introduction

Every month or so someone posts to comp.lang.python asking how Python compares to PHP. Python's 'language comparisons' page doesn't have an entry for PHP so here's a first draft. This work-in-progress compares PHP and Python and evaluates their relative strengths for web development.

Please contribute! Once there's some flesh on this skeleton, I'll tidy it up and submit it to: http://python.org/doc/Comparisons.html

-- TavisRudd? - 23 Nov 2001 -- MikeOrr - 15 Dec 2001 -- DiogoResende - 21 Jun 2007 :) -- SamuelCochran - 6 Jan 2008

Common Ground

Both PHP and Python:

  • are interpreted, high level languages with dynamic typing
  • are OpenSource (except PHP's ZEND engine?)
  • are supported by large developer communities
  • are easy to learn (compared to Java, and even Perl)
  • are easy to extend in C, C++ and Java
  • are extremely portable. They run on almost all platforms in existence without recompilation.
  • support for variable number of function arguments.
  • have the ability to freeze live objects in a string representation (for storing arbitrary objects on disk, moving them over the network, etc); they can then be converted back to identical objects with data intact. PHP's serialize function; Python's pickle and marshal modules. Note that PHP, handling of serialized objects and classes is much weakier and error prone than Python's due to PHP's lack of modules. When an object is serialized, only its attributes are stored, not its methods. Thus, the object's class must be present (with the exact same name) in the script that unserializes it. In Python this is handled automatically via the module/import framework. (this COULD be handled with PHP 5's __autoload(), but is not done automatically)

Compared as Languages

What does PHP have that Python doesn't?

  • syntax from C and Perl, with lots curly braces and dollar signs
  • the 'switch' statement and 'do ... while' construct
  • increment and decrement and assignment operators (assignment is a statement only in Python)
  • the ternary operator/statement (... ? ... : ...) (Python 2.5 added x = value if test() else other_value)
  • schizophrenic tableau of function names. The builtin library has a wide variety of naming conventions. 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, all variables are "set" (to NULL), and a somewhat weak type system (not to be confused with dynamic types) (global variables should not be used, that is language independent)
  • an expedient (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.
  • (An EXCELLENT on-line Manual)
  • (Class magic methods)
  • (A somewhat clean code. Where is the public/private/protected/final/.. atributes in python? Why do I have to always put "self" on class methods?)

What does Python have that PHP doesn't?

  • a general purpose programming language (not just for the web) (not true PHP has a CLI SAPI, and is embeddable) (PHP was created for the web but has branched into general purpose applications, however it's arguably not "good at/for it")
  • indentation is used to mark out block structure rather than curly braces
  • namespaces and modules
  • a small core
  • very clear, concise, and orthogonal syntax
  • it is self documenting with docstrings and pydoc (PHP 5 has reflection and doc strings)
  • keyword arguments to functions and methods, easy support for default arguments (PHP has easy support default arguments: http://uk2.php.net/manual/en/functions.arguments.php)
  • true object orientation and 'first class' classes and functions (OO has been completely revamped in PHP 5)
  • classes are used extensively in the standard library
  • a notion of private attributes (PHP 5 supports this)
  • multiple inheritance
  • object-oriented file handling
  • method chaining
  • excellent introspection (PHP 5 Reflection)
  • everything is a reference! (references are painful in PHP) (Not in PHP 5)
  • one 'del' statement for all data types. PHP has 'unset' for variables and something else for array members. (Array members can be unset())
  • consistent case sensitivity (PHP does for variables, but not functions) (Functions are case insensitive)
  • a simple array slicing syntax
  • lambdas and other builtin functional programming constructs (php.net/create_function - create_function is not a true lambda and pollutes the global namespace, particularly dangerous in any type of loop)
  • iterators (PHP 5)
  • structured exception handling (PHP 5)
  • operator overloading
  • SWIG? integration
  • threading
  • an excellent profiler, plus several debuggers and IDEs (PHP has had this for a long time)
  • lots of high-level data types (lists, tuples, dicts, mx.DateTimes, NumPy arrays, etc.)
  • differentiation between arrays (lists) and associative arrays (dictionaries) (that is not an advantage)
  • dates that aren't limited to UNIX timestamps (<1970,>2038)
  • cached byte-code compilation (available for $980 in PHP!) (pecl.php.net/apc -- FREE, but not standard amongst PHP hosts) (roadsend is now also open source)
  • a standardized database API (pecl.php.net/PDO -- PHP 5.1)
  • support for all major GUI frameworks
  • strong internationalization and UNICODE support (May be available in PHP 6, but only when enabled and requires an external package)
  • maturity, stability and upward-compatibility (PHP is stable, mature and up/backward-compatible - not cleanly?)
  • tends to lead to much more scalable applications -- importing modules is safer than textually including code as in PHP: global variables are not used to exchange information. (global variables should not be used, that is language independent)

Compared as Web Development Frameworks

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. Here's a simple classification of Python web development platforms:

  • emdedded in the web server process
    • Apache modules mod_python and mod_snake embed the Python interpreter in Apache and allow other Apache modules to be written with Python. This is the same idea as mod_perl.
    • PyWX is an extension to AOLServer that serves the same purpose as the Apache modules above.
    • MS ASP scripts can be written using Python via Active Scripting Host.
  • running in a separate process
    • non-persistent process (a new process is spawned for each request)
      • custom CGI scripts
    • persistent process (all requests are sent from the web server to one persistent process, sometimes via a CGI wrapper)
      • custom Fast-CGI scripts
      • custom SCGI scripts
      • custom WSGI? modules
      • 'Application Servers'
        • Zope -- comes with its own web server but can also communicate with Apache via the CGI or Fast-CGI protocols.
        • Webware? -- works with any web server via CGI. With Apache it can communicate via a custom Fast-CGI like protocol.
        • SkunkWeb? -- only works with Apache, communicates via a custom Fast-CGI like protocol.

(Note: PHP programs can be run standalone or as ordinary CGI scripts. However, when called indirectly from the web (e.g., from a Perl CGI script), it requires unsetting an undocumented number of web environment variables so that PHP doesn't think it's running from the web and look in the wrong place for the script. Likewise, it's _possible_ to write a long-running server in PHP, but there are precious few examples of it.)

The vast majority of Python web applications are run in a separate process. This has some important implications. ... to be continued ...

... please add your observations

Python Template Engines

...

Here's some interesting notes on security in PHP: (Not true. The tutorial assumes default conf values that are not default anymore or don't even exist) http://www.securereality.com.au/studyinscarlet.txt

Community Support

PHP

  • huge installed user base, but the figures are probably distorted by shared hosting (Not true. It has PEAR and PECL repository)
  • low signal-to-noise ratio -- because PHP is so expedient, many of the users are not invested in the technology (or even their own code) or the community (Not true. IBM and Oracle invested in PHP)

Python

  • sizable, but not huge, installed user base
  • Python Software Foundation
  • lots of specialized interest groups
  • very high signal-to-noise ratio

Documentation

Core Documentation

Although both PHP and Python have excellent core documentation, Python's is more extensive and generally higher quality. PHP has a large number of translations available. Python doesn't. For PHP see http://www.php.net/docs.php and for Python see http://python.org/doc/ Python allows documentation on modules, classes, and functions to be included in the program code. The documentation becomes an attribute of the module/class/function, accessible from inside of the language itself. (This is so untrue. Python docs are short and don't explain in detail how something can or should be used. PHP uses examples (L.B.E.). PHP has comments on every page so you can check for someone that tried to do the same thing you did. PHP can be documented just like many other languages like C for example; http://www.phpdoc.org/ )

Frequently Asked Questions

Q1

... what is the best way to build web-based database applications like accounting systems, on Python? i.e. what is the equivalent of ASP pages or PHP pages or JSP pages, for Python?? Shouldn't we be learning THAT instead of PHP???

A1

There is no simple answer to this question. There are many tools available for doing this in Python. So many that the choices can be bewildering. You're best to think of the tools as falling into two complementary categories: Application Servers (there's probably a better term to use here) and Template Engines.

Application Servers:

Templating Engines:

My personal preference is Apache/Webware/Cheetah. I'll write more about this later. -- TavisRudd? - 23 Nov 2001

General Discussion

(add your comments here, along with a signature and date)


I haven't used PHP with XML, and I've only used Python with XML a little, but I get the impression Python is much better suited -- specifically because it is strongly types, where PHP is rather weak in its typing (like C). But I can't say from experience

-- IanBicking - 26 Nov 2001


To clarify, PHP's types are more dynamic than Python's. All variables are automatically initialized to NULL. It automatically converts between strings, numbers and NULL when a different type is needed for the operation, or with a type case. It also "does something" to convert to/from array and object. '123' -> 123, 'non-numeric' -> 0, NULL -> '', etc. Sometimes all this is convenient, especially when a missing CGI param converts to NULL which converts to '' in the VALUE= attribute, but it also leads to wrong ouput rather than errors if you misspell a variable or key.

PHP does not have first-class functions. There are equivalents to Python's map, reduce, filter, and there's array_walk, but they all take the name of a function rather than the function itself.

Examples of PHP's schizophrenic function naming: (string functions) stripslashes vs strip_tags; (array functions) array_keys vs asort vs count and shuffle; (variable functions etc) isset vs is_array.

PHP has constants, which are initialized with a totally different syntax than variable assignment. The advantage of constants is that they can be defined only once, allowing you to define sensitive constants in a secure include file, then prevent untrusted code from overriding them. Constants are global: they bypass the scoping rules. One disadvantage is that a constant is a bare word: if you misspell it, you'll silently get a NULL value.

Syntax errors show the file and line number, but not the character position. Sometimes the error message is specific, sometimes just "parse error". This leads to staring at the code for a while until you realize you're missing '$' or ';', or you put something besides a semicolon on the END line matching <<

-- MikeOrr - 15 Dec 2001

No comments: