PHP vs Python: A Decision

Page created by Virgil Mclaughlin
 
CONTINUE READING
PHP vs Python: A Decision
                                       Greg Rauhöft

                        Medien- und Kommunikationsinformatik
                                Hochschule Reutlingen
                     greg.rauhoeft@student.reutlingen-university.de

      Abstract: The goal of this document is to offer a direct comparison of the
      programming languages PHP and Python and to check if an empirical and
      situational decision can be made about the superiority of one of the languages over
      the other. The findings of this study are based on the current stable releases of
      these languages, which are PHP 5.3.6 and Python 3.2., as these are likely to present
      the current state of the art of their underlying technologies. The conclusions is
      based on a number of different factors that can be deemed useful for deciding what
      language to choose for a certain purpose. The research covers a wide area of
      relevant topics instead of focusing on a single one.

1. Preface

To understand the status quo of PHP and Python it may be useful to first know about the
origins of both languages.

1.1 A Brief History of PHP

PHP was initially developed as a set of PERL scripts to create simple dynamic websites
by Rasmus Lerdorf in 1995 [PHP2011-26]. After a reimplementation and expansion in C
and a source release, it quickly gained popularity to be present on 1% of the internets
domains by 1997 [PHP2011-26]. Andi Gutsman and Zeev Suraski decided to collaborate
with Lerdorf to improve PHP, as they considered it underpowered for eCommerce use,
resulting in the development of PHP 3, which is the basis of what PHP is today
[PHP2011-26]. In 1998 Suraski and Gutsman rewrote the PHP core and developed the
Zend Engine [PHP2011-26]. By 2001 PHP had developed into more than a language for
dynamic websites with the first release of PHP-GTK, which enables developers to write
standalone GUI applications in PHP [PHP2008]. Today, with PHP 5, it runs on the Zend
Engine 2.0 [PHP2011-26].

                                               1
1.2 A Brief History of Python

Python was developed from its conception by Guido van Rossum in 1990 [PSF2011-24]
as a general purpose, open source [PSF2011-25] scripting language. In the year 2001 the
Python Software Foundation (PSF) was founded [PSF2011-25] as a non-profit
organization to “promote, protect and advance the Python programming language”
[PSF2009]. 2008 saw the latest major Python release with Python 3.0 [PSF2011-25] that
also broke backwards compatibility [PW2011]. Although the Python 2.x branch is still
being developed, it will be phased out and replaced by the Python 3.x branch [PW2011].

1.3 Introduction

Albeit PHP formerly being a specialized language for the easy creation of dynamic
websites, it has steadily developed in to a general purpose language that matches Python
in feature completeness and maturity. While PHP is still mainly used as a server side
scripting language it is no longer described as only such on its official website, also
citing its CLI and PHP-GTK features [PHP2011-27]. Python was developed as a general
purpose language from scratch so it has never been pressed in to a certain niche. Due to
these circumstances this study will focus primarily on what PHP has gained on Python
regarding general purpose programming. Only in server-side scripting sector will we
concentrate on how Python matches up with PHP. Concerning the general language
features both languages will be treated as equal.
To compare both languages, certain aspects, such as language features and uses common
to both languages, will be compared directly. This is by no means complete or definitive,
but merely an overview, as comparing all features would exceed the limitations of this
document.
The criteria for the choice of language aspects to compare is based on practical
considerations to be taken towards the application of programming languages in general.
This is separated in to two parts: General features belonging to the language and uses of
the languages.
In the overview of the language, documentation was considered a comparable aspect, as
a poorly or incompletely documented language is harder to use, cannot be utilized in its
entirety and the outcome of a program may be unexpected, creating an insecure liability
for the producers and customers alike. In addition, the syntax of the languages was taken
in to consideration, as a clear and simple syntax can make it easier for other people to
read and understand the code, making code reviews and programming in a team easier.
To provide insight in to alternative usage scenarios of the language, a review of the
various implementations of both languages is provided. In more technical terms, garbage
collection is an aspect of a language that is especially important for long running
programs that frequently create objects. As such it should be taken in to consideration
when comparing languages for general programming use. This goes hand in hand with
the languages type system, as a weak typing system, especially in an object oriented
language, can lead to errors that are not necessarily obvious and, as such, hard to find. As
dynamic languages are often used as embedded scripting languages that may also require

                                             2
an extended feature set, embeddability and extensibility will also be taken in to account.
The final aspects taken reviewed in the language section are the programming paradigms
that can be used with the language and how these can be utilized. These include the
object oriented (including instances and inheritance), functional and concurrent
paradigms.
Concerning the usage of the languages, the most important one for comparison purposes
may be with web servers, as this is where PHP is most commonly used. Regarding the
LAMP stack the comparison will go in to more detail, as the “P” in LAMP can stand for
PHP or Python (or PERL for that matter). The CLI features of the languages will also be
considered, because this most basic form of human-computer interaction supplies output
for developers and can be an important debugging and testing tool. Not to mention the
programs that actually use the command line for all their interactions, as they may lack a
GUI. To expand upon this, the use of the languages for standalone applications will be
compared, as the use of a general purpose programming language should go beyond
being embedded or called to perform a certain task for its host program. To perform any
general work in a production environment, a rich set of libraries should be available to
perform various tasks. Rewriting everything a project may require is not a feasible task
and a waste of time, so an overview of a diverse set of libraries will also be given.
As seemingly no citation-worthy literature comparing PHP and Python has previously
been written, most points of criticism are taken from examining their respective official
documentation and websites and comparing these.

2. Language

2.1 Documentation

PHP has a single documentation that covers all versions from 4.1.0 to 5.x.x [PHP2011-
1]. Version specific information is on the same pages as general information. Despite
being very extensive, covering all the topics necessary to understand the language, it
lacks a formal language and grammar description in a BNF-like way. This can only be
acquired by downloading the PHP source code and reading the parser specific source
files contained within it. At the time of writing large parts of the documentation were
also incomplete, especially regarding the internals of the Zend Engine [PHP2011-2]. The
PHP documentation also features a system for user contributed notes, which allows users
of the language to add their own experiences about the presented topic, giving hints to
pitfalls and common usage scenarios [PHP2011-3].
Python has separate documentations for every version, salvaging the documentation of
previous versions for unchanged features. It uses a modified BNF grammar notation for
lexical analysis and syntax description. [PSF2011-1]
As the documentations for both languages are very extensive and feature examples and
tutorials, it makes it easy for programmers to learn the languages. The large library
references enable programmers to extend their knowledge and solve specific tasks
quickly with the tools the languages have to offer. Although PHP's policy of having one

                                            3
documentation for all versions makes it easy to see where differences occurred, it does
clutter up the pages and since programmers are likely to use one version of a language to
perform one specific task, the Python documentation is more concise. Both the PHP and
the Python documentation offer lists of update notes that show where differences to
previous versions are. This is likely to be the better approach.
The lack of an easy way to access PHP's formal syntax description makes it
unnecessarily hard to fully understand how the language works in detail and to
implement custom parsers or syntax highlighters, and the lack of proper documentation
of the Zend Engine's internals makes it unnecessarily hard to extend and embed PHP. It
seems PHP's lacking official documentation is compensated through a large amount of
third party books. A search on amazon.co.uk in the “Computing & Internet” section
returns 8703 results for “PHP” and only 3209 results for “Python” (data from
11.08.2011). Interestingly enough, a search of the “IEEEXplore Digital Library”
(http://ieeexplore.ieee.org) returns almost the same amount of results for “Python” (270
results as of 12.08.2011) as it does for “PHP” (285 results as of 12.08.2011) in the
“Computing and Processing” section, hinting that both languages garner about the same
amount of academic interest.

2.2 Syntax

PHP was designed to be easily embeddable in documents containing other data than only
PHP code. It is for this reason that even a pure PHP file contains a slight overhead in
tags marking the beginning and end of a PHP section (commonly 
respectively). The trivial case of a pure PHP file will have the opening tag right at the
beginning and the closing tag right at the end of it while a file can be filled with an
otherwise arbitrary number of opening and matching closing tags, as the PHP parser
ignores (or outputs) everything outside of them [PHP2011-4].
The general syntax features elements from C, Java and PERL and as such is dominated
by C-like braces, Java-like keywords and PERL-like variables [PHP2011-5]. The PHP
grammar contains inconsistencies in case sensitivity though. As such “the variable names
are case-sensitive but the function names are non-case sensitive” [Cho08, p. 3].
A very different approach was taken with Python's syntax. Instead of using braces for
structuring code, it uses different levels of indentation. This causes Python to enforce an
implicit coding style, as variations in indentation can either lead to errors or to semantic
differences [PSF2011-2]. Furthermore, many of Pythons operators can either be written
as symbols or as words, enabling programmers to adopt a coding style that looks and
feels more like a set of English sentences [PSF2011-3].
While PHP's syntax makes it easy for Java and C programmers to migrate to PHP, due to
it's familiar look and feel, it does nothing new for readability. Due to Python forcing a
certain coding style upon programmers it can make code easier to maintain, as it isn't
necessary to get accustomed to another programmer's coding style when reading foreign
code. Indentation is a common coding style taught in programming courses and can
make code easier to read. Python's dependence on indentation removes an error source
that remains in PHP.

                                             4
2.3 Implementations

The standard Python implementation is called CPython and is, as the name might imply,
written in the C language. [PSF2011-1] To broaden Python's range of usefulness, several
other Python implementations were developed, targeting specific platforms, including
Java with Jython and the .NET Framework with IronPython. These platform-specific
implementations were written in the corresponding target platforms primary languages,
Java [JWBSN2010] and C# [IP2011] respectively, and make use of their runtime
environments, the Java Virtual Machine [JWBSN2010] and Microsoft's Common
Language Runtime [IP2011]. As such, Jython and IronPython have access to their host
environments libraries and vice versa [JWBSN2010][IP2011].
A different approach was taken by the developers of the PyPy Python implementation.
PyPy's implementation language is a subset of Python its self, called Rpython, which can
be converted to C code and compiled natively [PP2011-2]. It claims to be faster than
CPython in many benchmarks, as it uses Just-In-Time compilation or can be used
stackless to run with micro threads [PP2011-1]. Furthermore PyPy can also run on the
Java Virtual Machine and in Microsoft's Common Language Runtime [PP2011-1].
Stackless Python is a Python implementation that was specifically written to “reap the
benefits of thread-based programming without the performance and complexity
problems associated with conventional threads” [SL2011-1]. This subject will be
covered in more detail in the section of this paper about concurrency.
The standard implementation of PHP comes in the form of the Zend Engine 2 as of PHP
5 [PHP2011-6]. It was written in the C language. PHP also has a range of alternative
implementations opening the language to different usage scenarios. Project Zero, a
“technology incubator project” [PZ2008-1] for IBM's “WebSphere sMash” [PZ2008-1]
contains an implementation written in Java [PZ2008-1], that still lacks the PHP function
library, as the focus was on “language level constructs” [PZ2008-1] up to now. It does,
however, allow programmers to write PHP extensions in Java [PZ2008-1].
Phalanger is a compiler for PHP, that translates PHP into the Microsoft Intermediate
Language to run on Microsoft's Common Language Runtime [PH2011-1]. As such, it
also allows for using objects written in other .NET languages to be accessed from PHP
and vice versa [PH2011-1].
Developers at facebook wrote a compiler for the PHP language called “HipHop” that
allegedly “transforms PHP source code into highly optimized C++” [HH2011-1], while
removing and replacing certain features of the language [HH2011-1].
PHP's lack of a formal syntax description makes it hard to write other implementations
of the language, which is evident by the lack of complete alternative implementations,
thus limiting PHP's usefulness as a multi-purpose scripting language. Python's
implementations allow for a wide range of usage scenarios and can easily be used in
projects that require the cooperation of multiple programming languages.

2.4 Garbage Collection

Python's main method of garbage collection is by reference counting [PSF2011-4].

                                           5
Every object has a counter for the number of variables referencing it [PSF2011-4]. As
soon as the reference count reaches zero, the memory is freed [PSF2011-4]. In addition
Python also has a cycle detector to detect if a reference to an object is directly or
indirectly to its self [PSF2011-4]. Once detected it can free these cycles as soon as they
aren't referenced from anywhere else [PSF2011-4].
Python's garbage collector can be controlled, activated or deactivated and run via its
Garbage Collector Interface, called “gc” [PSF2011-5]. In addition it “provides access to
unreachable objects that the collector found but cannot free” [PSF2011-5] as well as
access to a list of all objects tracked by the collector [PSF2011-5]. Furthermore,
debugging of the garbage collection process can be enabled from within Python, offering
information about leaks, collectable and non-collectable objects and general collection
statistics [PSF2011-5]. The collection threshold that decides when the garbage collector
is run can also be set at runtime [PSF2011-5].
PHP also uses reference counting for garbage collection along with cycle detection for
self referencing objects [PHP2011-8]. It frees memory as soon as a request ends or the
reference count reaches zero [PHP2011-8]. PHP user control for the garbage collector is
more limited. It only allows to be instructed to be enabled, disabled or run [PHP2011-7].
By default the garbage collector in PHP runs when 10.000 roots are created [PHP2011-
8]. This constant cannot be altered at runtime or in any PHP settings. It requires a change
in PHP's source code and a subsequent recompilation [PHP2011-8].
Despite PHP and Python using similar approaches for garbage collection, the greater
control Python offers over the process allows for more precise optimization of it. The
information Python's garbage collector offers at runtime can allow for dynamic
collection optimization and highlight leaks and shortcomings in efficiency that aren't
currently possible with PHP's implementation. Also, the fact that PHP's collection
frequency cannot be tweaked, except through the recompilation of the entire source
code, makes PHP less suitable for long running applications, such as GUI or server
applications.

2.5 Type System

PHP is a weakly typed language, which allows for something the PHP manual refers to
as “type juggling” [PHP2011-9] which is the implicit conversion between various
primitive and abstract data types through operators [PHP2011-9]. As such, PHP throws
no exception and generates no warnings if, for example, the instance of a class is added
(+) to the instance of another class, which produces the counter-intuitive result of an
integer with the value of 2. PHP also allows for explicit type casting to primitive types
following a syntax equivalent to the one used in C. This can also lead to counter-intuitive
results, such as the conversion of the empty string or a string with the value of “0” both
evaluating to false if casted to a boolean [PHP2011-10].
Python uses strong typing as well as duck typing as its typing disciplines. So in spite of
operations on primitive types requiring explicit type conversion (e.g. int(“5”)+5)
[PSF2011-8], methods with the same names and signatures from different classes can be
called in the same way without prior casting and without sharing a common base class

                                            6
declaring said methods [PSF2011-7]. This allows for a kind of polymorphism that
doesn't rely on inheritance [PSF2011-7], which can flatten an inheritance tree if used
accordingly. Type conversion works in an object oriented manner in Python, as the type
conversion operators are simply calls to the corresponding built in types constructors
(such as int()) [PSF2011-6, int].
Seeing as PHP was initially developed for server side scripts where security is crucial,
it's weak typing discipline seems dangerous and out of place, as errors caused by
unchecked and accidental type conversions are not only hard to find but can also lead to
unexpected behaviour and results [Cho08, p. 1]. Python's approach inhibits this kind of
danger by requiring explicit type conversions, thus making it harder for unwanted or
accidental conversions to go unnoticed. Furthermore, Python's approach to type
conversion via constructors instead of a separate language construct as it is in PHP is
more consequent and fits into the nature of the language, where everything is an object.

2.6 Extensibility

Python offers two distinct ways to extend the language. The first one is through using
Python's C API to define a new module [PSF2011-10]. This enables Python to call native
C code [PSF2011-10]. The second one is through using Python's “ctypes” module, which
is part of its Generic Operating System Services [PSF2011-9]. This module “provides C
compatible data types, and allows calling functions in DLLs or shared libraries”
[PSF2011-9], thus allowing programmers to wrap functionality written in C in Python
code.
Similarly to Python, PHP's Zend engine also offers a C API for programming extensions
in C and allows the calling of C code from within PHP [PHP2011-2]. As large parts of
the documentation about the Zend engine's internals are missing [PHP2011-2] at the time
of writing, no further judgement can be made about it at the current time.
Extending PHP and Python requires a similar amount of effort from sides of the
programmer and works very similarly in code. Python's ctypes module, however, can
make the portation of existing libraries significantly easier. The lack of a complete
documentation for PHP's Zend Engine's internals makes learning the skill of extending
the language more troublesome than with Python.

2.7 Embeddability

Python offers two separate ways to be embedded in other applications. The first one is
called “Very High Level Embedding” [PSF2011-11]. It allows a string of Python code to
be sent directly to the Python interpreter without using system calls [PSF2011-11]. This
doesn't allow for direct interaction with the Python application though [PSF2011-11]. To
achieve this, Python offers a method called “Pure Embedding” [PSF2011-11]. It works
like extending Python, with the exception that Python code is called from C code instead
[PSF2011-11]. Through the combined use of “Pure Embedding” and Pythons C API,
mutual communication between the two languages can be established.

                                           7
Embeddability is an undocumented feature of PHP at the time of writing. The source
distribution of PHP contains a SAPI called “embed”, yet it contains a warning to users
about it being experimental and likely to change or be converted to an extension
[EK2011].
As embedding PHP is undocumented and still in an experimental phase, Python should
be the language of choice for anyone wanting an embedded language to enhance the
functionality of a program.

2.8 Paradigms

2.8.1 Object Orientation

Despite supporting classes, Python's default means for data encapsulation are lacking.
Python doesn't contain a concept of protected members and private members are simply
preceded by two underscores ( __ ), which cause the members names to be combined
with the class name before code generation to form a name resembling
_classname__membername [PSF2011-12, private]. The consequence of this is that the
access to private members isn't actually forbidden, as the member can still be accessed
through the mangled name. It is solely at the programmer's discretion to avoid accessing
private members [PSF2011-12, private]. Python also offers properties similar to those
seen in C# as a means of access control or member protection as well as for clean-up and
creation actions [PSF2011-6, property]. These properties still rely on a private member
variable to be used, which can still be altered by the aforementioned method.
Furthermore, member methods have to explicitly name the reference to their containing
instance as their first parameter, leading to redundancy in code [PSF2011-12, Random
Remarks]. When calling a member method, it is not necessary to explicitly pass the
containing instance, as Python considers an instance.method() call to be the equivalent of
class.method(instance) [PSF2011-12, Method Objects].
Python allows the definition of meta-classes to customize class creation [PSF2011-13,
Customizing Class Creation]. Every class has a meta-class by default, but allows for new
ones to be written and assigned and are also inherited from base classes where applicable
[PSF2011-13, Customizing Class Creation]. The customization features include the
control over instantiation of classes [PSF2011-13, Customizing Class Creation] and even
to completely deny it (by overwriting the __new__ function), as well as controlling the
inheritance from base classes (by overwriting the __prepare__ function) [PSF2011-13,
Customizing Class Creation].
The PHP approach to object orientation closely resembles that of Java. Encapsulation
modifiers are placed directly in front of the member and include public, protected and
private [PHP2011-11]. These work as expected with public being the default modifier
and visible inside the instance as well as from the outside, protected being visible to
subtypes and private being visible only to the containing instance [PHP2011-11]. As
PHP forbids identifiers from standing alone, modifiers also offer a means of declaring a
variable without having to initialize it [PHP2011-12]. PHP's encapsulation features
appear to be rather secure, with the only obvious means of circumventing them being

                                            8
reflection.
The judgement over which programming language offers better features for Object
Orientation depend on whether or not strict encapsulation should be considered a
language feature or a project specific programming guideline. Considering the dynamic
nature of both languages, that encourage programmers to take different approaches to
solving problems efficiently and easily, the non-strict variant that Python takes is likely
to work out better. Furthermore, Python allows for complete control over class creation
by using meta-classes, so specific rules can be implemented depending on its use. PHP
lacks this feature.

2.8.1.1 Instances

In Python instances of classes can easily be extended and functionality altered by simply
assigning a value to an arbitrary identifier. Where x is not an element of the class's
members and y is an expression, the following statements adds a member to this specific
instance: instance.x = y [PSF2011-14]. The same statement with x as a member of the
class would override the instance's x with the result of y [PSF2011-14]. As much as this
offers a very dynamic tool to work with, it can also create problems. Typographical
errors on the left side of an assignment do not lead to an error message by default, thus
being prone to errors which can be hard to debug. It also offers a way to extend classes
without the use of inheritance, functionality can be altered unknowingly. This
functionality can be disabled by defining an attribute setter and deleter (__setattr__ ,
__delattr__ [PSF2011-13, Customizing Attribute Access]) for a given class. If the
functions body is left empty, it prevents the assignment of a value to a previously non-
existent identifier, thus preventing the alteration of an instance.
Instances in PHP can also be altered by means of assigning a value to an arbitrary
identifier that isn't contained in the set of class variables of the instance's class
[PHP2011-13]. This counts for assignments within as well as outside of the instance its
self. If a variable is declared as private or protected and an attempt is made to change its
value from outside of the instance, the program ends with a fatal error [PHP2011-11] , as
a Java or C++ programmer would expect. Similarly to Python's behaviour,, PHP offers
magic methods that can be used to bypass this restriction. Defining a general attribute
getter and setter for the class (__set, __get [PHP2011-13]) to simply assign a passed
value to an identifier, or return the value of the requested identifier respectively, breaks
any access modifiers [PHP2011-13]. Like in Python, an empty function body can be
used to ignore any assignments to identifiers and create an instance filled with constants.
Considering the dynamic nature of the PHP language, it seems illogical to allow adding
variables to an instance from the outside but restricting this feature to variables that
aren't declared as private or protected within the original class, as these shouldn't be
visible from the outside anyway. Python takes this into consideration through its name
mangling mechanism, as private members receive an internal name that differs from the
name they originally received in the class definition, thus not restricting these kinds of
assignments in PHP's manner. PHP's unnecessary confirmation of the existence of
internal private or protected variables seems to have passed over from Java or C++

                                             9
without taking the actual necessity for this into consideration. Using PHP's magic
methods, it should be possible to create a custom name mangling system similar to the
one present in Python.

2.8.1.2 Inheritance

Python supports the concepts of inheritance [PSF2011-12] and multiple inheritance
[PSF2011-12] and allows for overriding inherited methods and attributes [PSF2011-12],
and, due to its dynamic nature and unlike in Java, inherited class features can be
removed. This goes hand in hand with Python's two type-checking functions isinstance
and issubclass, to check if a variable is of a certain type or subtype [PSF2011-12]. To
support multiple inheritance, Python uses depth-first, from left to right search in order of
base class declaration to find members throughout the base classes [PSF2011-12]. This is
enhanced by a dynamic restructuring of the base classes to avoid the diamond
relationship problem [PSF2011-12]. The concept of abstract classes can be implemented
through meta-classes and a pre-implemented abstract base class meta-class exists in
Python's standard library [PSF2011-15].
PHP also supports inheritance using the same syntax present in Java, through the extends
[PHP2011-15] keyword, but like Python, inherited members can be forgotten if
necessary. If a class is derived from another, it inherits all public and protected members
of the base class [PHP2011-14]. Abstract classes can be created using the abstract
keyword, preventing underived instantiation [PHP2011-16]. Also like Java, interfaces
can be defined in PHP, allowing a programmer to declare a set of unimplemented public
functions to be implemented by any class using the interface [PHP2011-17]. At present,
PHP offers no predefined means for multiple inheritance.
PHP's inheritance mechanism is closely modelled on Java's (including the keywords),
thus inheriting some of the shortcomings of Java's inheritance mechanism, such as the
lack of a means for multiple inheritance. The benefit of this is an easy transition for
programmers coming from Java or C# to PHP's approach to object oriented inheritance.
Despite Python not sharing any inheritance specific syntax elements with Java, thus
creating a shallower learning curve for people coming from a Java background, its
intuitive and logical means for multiple inheritance make it more versatile than PHP,
especially if the use of multiple inheritance is deemed necessary.

2.8.1.3 Exception Handling

Python uses a form of exception handling that should be familiar to anyone who knows
Java. An exception is a class that can be extended to create custom exceptions.
Exceptions can be thrown and caught [PSF2011-26]. An uncaught exception leads to the
termination of the program [PSF2011-25]. What separates Python from Java is the
syntax of Exception controls. Exceptions are thrown by “raising” them (raise keyword)
and caught by “excepting” (except keyword) [PSF2011-25]. The finally block is the
same as in Java, which is executed regardless of whether an exception occurred

                                            10
[PSF2011-25]. Vulnerable code sections, expecting a possible error are placed in a try
block [PSF2011-26]. Python also has built-in exceptions that are thrown if a runtime
error occurs [PSF2011-26][PSF2011-25].
PHP doesn't only share the usage of Exceptions with Java, it also shares the syntax. They
are thrown my using throw, caught by using catch and expected with a try block
[PHP2011-28], but PHP completely lacks the finally block [Dop10, p. 287]. Furthermore
exceptions were not always a part of the PHP language, only being introduced in version
5 [PHP2011-28]. This leads to the problem that PHP has two kinds of runtime errors:
those that can be caught, using the new exception system and those that simply display a
warning or cause the program to crash in the worst case [PHP2011-28].
Despite Pythons syntactical differences, its usage should be familiar to Java and C#
programmers, while PHPs lack of certain exception handling features can be unintuitive.
The lack of a finally block denies a programmer the possibility to define a block of code
to be executed regardless of whether an error occurred, which can lead to code
duplication for the normal program path and the catch block as well as instability in long
running programs [Dop10, p. 287]. Furthermore, PHPs inconsistent use of catchable
exception creates a possible cause of program crashes that is harder to avoid at runtime.

2.8.2 Functional

PHP supports closures which lays the foundation for programmers to use the functional
paradigm [PHP2011-18]. Functions can accept other functions as parameters and are
also able to create and return functions [PHP2011-18]. PHP doesn't require a name to be
given to a function [PHP2011-18] or for it to be predeclared.
Python supports anonymous functions in form of lambdas [PSF2011-16]. This
functionality is restricted by allowing lambda expressions only to contain other
expressions and not statements [PSF2011-16]. But as the pure functional paradigm
doesn't include states, Pythons lambda enforces this rule. Lambdas are currently the only
way for anonymous functions to be created in Python, but named functions may be
passed as parameters as well, yet have to be predeclared [PSF2011-17].
PHP's anonymous function mechanism may offer the programmer greater flexibility, but
defining functions in this manner can easily lead to code that is hard to read. Due to
Python restricting it's anonymous functions to contain only expressions and not
statements, it keeps them short, easier to read and avoids state changes. The enforcement
of the expression only rule also makes Python's lambdas suitable for academic use, as
they work exactly how a student of the lambda calculus would expect and preserves the
rules of pure functional programming.

2.8.3 Concurrency

Concurrency is featured in Python as “Optional Operating System Services” [PSF2011-
18], meaning it is only available on certain operating systems. Python's standard
implementation, CPython, contains a performance based restriction called “Global

                                           11
Interpreter Lock” [PSF2011-18]. This restriction only allows one thread to execute
Python byte code at a time [PSF2011-18]. As such, concurrency is only possible by
means of multiprocessing (using Python's multiprocessing module [PSF2011-19]) by
default [PSF2011-18].
The aforementioned Stackless implementation of Python adds full threading support to
the language in form of microthreads (“Tasklets” [SL2011-1]), including communication
between threads [SL2011-1], scheduling and thread serialization [SL2011-1].
Concurrency in PHP is disabled by default and must be compiled with a certain switch to
activate it [PHP2011-19]. It is only available on Unix platforms and only by means of
multiprocessing [PHP2011-19]. PHP does, however, allow multiple copies of PHP to run
in the same thread (the opposite of Python's “Global Interpreter Lock”), so care must be
taken when defining global variables when writing extensions for PHP [PHP2011-20].
Despite Python's standard implementation not supporting true threading for good
reasons, the Stackless implementation makes up for these shortcomings. PHP has
nothing to offer regarding true threading and neglects the danger of hard to find errors
which comes with global variables when running several instances in the same thread.
Furthermore, according to Cholakov, PHPs lack of true multithreading “is going to be a
major vulnerability for the platform in the very near future” [Cho08], as “reasonable
(and constantly dropping) prices of these components give a good sign that even the
smallest companies will tend to use multiple cored Web servers” [Cho08].

3. Uses

3.1 Web Servers

Not necessarily a language feature, a Python enhancement proposal was created and
approved in 2003 [PE2003] and extended to improve usability in Python 3 in 2010
[PE2010] to create a Web Server Gateway Interface (wsgi) specification “for web
servers and application servers to communicate with web applications” [WSGI2010]. It
is supported by a number of web-frameworks, such as Django, Pylons, Zope 3
[WSGI2011] and Apache (through mod_wsgi [GD2011]). Python contains a reference
implementation of the wsgi specification that can assist in writing wsgi enabled
applications [PSF2011-20].
PHP provides a range of SAPIs to communicate with servers that are included in the
official source distribution [PHP2011-21]. These include Apache, Litespeed among
others and hook directly into the specific servers [PHP2011-21].
PHP's compatibility with Web Servers is where it excels, probably because it was
initially designed to perform web-based tasks. But while PHP's SAPIs have to be
rewritten for every server, Python's WSGI aims at offering a common interface for
application-server and server-server communication.

                                          12
3.1.1 LAMP

Python support for the LAMP (Linux, Apache, MySQL, PHP/Python/Perl) stack was
initially introduced by the mod_python project, allowing the Python interpreter to run as
a module in Apache [AS2010]. The Apache Quetzalcoatl project was “charged with the
creation and maintenance of open-source software related to mod_python and the Python
programming language” [AS2011] , but was cancelled due to community inactivity
[AS2011] . The official mod_python project page claims that the project isn't dead, but in
a state that is mature enough to require very little maintenance [AS2010].
An alternative is to use mod_wsgi, an Apache module that supports Python's Web Server
Gateway Interface [GD2011].
PHP's Apache module (mod_php) is provided along with the PHP source code
[PHP2011-21] and can be connected to Apache after being built with switches to enable
Apache specific extensions [PHP2011-22]. A pre-compiled set of Apache, MySQL and
PHP/Perl comes in form of XAMPP for various operating systems to make the
installation easier.
The Apache mod_python seems to be obsolete as it's parent project was cancelled, while
mod_php is still the most commonly used Apache module. The mod_wsgi, however,
offers consistency for the communication with any web-server that implements it, which
makes it an interesting alternative.

3.2 CLI

By default Python has several ways it can be used with a command line interface. The
Python executable run without parameters puts Python in an interactive console mode
[PSF2011-21]. This allows programmers to write a line of code and receive direct
feedback about syntactical correctness as well as the result of any expression entered
[PSF2011-22]. In this mode it can also be used as an advanced calculator [PSF2011-22]
due to Python's large mathematical library. Anything written into the console during a
session persists until the session ends [PSF2011-22].
The Python executable followed by the path of a Python file runs the contained program,
which can then access the standard in- and output and use it for user interaction
[PSF2011-21].
The IDE that ships with Python, IDLE, comes with a built-in command line [PSF2011-
23]. The functionality is the same as Python running in the interactive console mode,
with the additional features of being able to retrieve previous commands, as well as the
ability to save the interactive session, which can be fully recalled [PSF2011-23].
PHP's CLI SAPI is the default to be built along with the CGI SAPI when configuring
PHP without options [PHP2011-23]. Like Python, the PHP executable can be followed
by the path of a PHP file to run the contained program [PHP2011-25]. The CLI can also
be run with the -a option to receive an interactive shell [PHP2011-24]. The precondition
for this is that PHP was compiled with the –with-readline option, otherwise the shell will
not be able to pass the input to the PHP interpreter [PHP2011-24]. It also contains a
peculiar exception to PHP's opening and closing brackets rule, in that commands sent

                                           13
directly to the interpreter in this manner don't have to be surrounded by them [PHP2011-
24].
Python's CLI features are very sophisticated and work out of the box while PHP's require
a specific configuration before compilation to offer a similar amount of features.

3.3 Standalone Applications

Beyond the previously mentioned CLI uses of PHP and Python, both languages have
several other ways they can be used to write standalone applications, mainly in
combination with external libraries. These include various GUI toolkits that will be
discussed in the libraries section of this document.

3.4 Libraries

As Python 3 broke backwards compatibility with previous versions, thus also breaking
compatibility with many previously available libraries (modules in Python), the
following section will ignore whether or not the mentioned libraries are Python 3
compatible or not.
There are a number of modules to provide the use of GUI toolkits, such as GTK with
PyGtk [PyGTK2010], Qt with PyQt [RBC2010] or the official PySide [NOK2009] and
Tkinter for Tcl/Tk [TKI2011].
Furthermore there are also libraries to enhance Python's abilities to be used for scientific
calculations, such as SciPy . It offers a wide range of functionality for “mathematics,
science, and engineering” [SPY2011-1], including clustering, fourier transforms, linear
algebra, image processing, optimization, statistic and signal processing [Oli07, 19-20].
Similarly, Scientific Python is a collection of modules for scientific computing that
includes (but is not limited to) automatic derivative calculation, a Fortran interface, 3D
Vectors and tensors including quaternions, histogram generation and 3D visualisation
modules [KH2007].
RPy is a Python interface for the R Programming language [RPY2010] which allows
easy access to statistical computing and graphics [RP].
Python also has a range of libraries aimed at facilitating the generation of games and
interactive environments. These include Pygame, which is a wrapper around the SDL
library [PG], Python-Ogre, which allows the use of the open source Ogre3d engine [PO]
through Python and PyOpenGL, which provides Python bindings for OpenGL [PGL].
Targeting web applications a number of frameworks were developed. Django is a web
application framework that focuses on rapid development and widespread automation
and includes an object-relational mapper and an automatic admin interface [DP2011].
The Pylons Project is a project aiming to release a number of technologies related to web
application frameworks, the current one being Pyramid [AC2011]. It aims to be simple
and minimalistic and to make it easy for developers to write web applications [PP].
Despite the fact that PHP is normally used as a server side scripting language, several
libraries have been developed to facilitate PHP's use as a general purpose language, such

                                            14
as libraries to allow the creation of Qt (through php-qt [PHPQT]) and GTK (through
php-gtk [PHP2009]) GUIs using pure PHP. These libraries are a bit outdated though, as
the latest GTK version supported by php-gtk is 2.12 [PHP2008] at the time of writing
while the latest versions of the GTK are 2.24 and 3.0 [GTK+2011] and the php-qt project
is still in its beta phase while it's latest release dates back to July, 2009 [PHPQT2009].
Web development is still where PHP excels though, offering a number of frameworks
and content management systems for this purpose. These include Joomla [OSM2011],
Media Wiki [WMF2007], Drupal [DRP], TYPO3 and WordPress [WP], which were all
written and can be extended through the use of the PHP language. Joomla is a free and
open source CMS and application framework, whose official website claims that it
powers 2.7% of the web [OSM2011]. WordPress is a publishing system and CMS aimed
at blogging, but is suitable for general purpose use [WP]. The Media Wiki is a free and
open source wiki that is most famously used for Wikipedia [WMF2007]. Drupal and
TYPO3 are both content management and publishing systems released through the terms
of the GPL [DRP][T3A2011].
While Pythons selection of libraries is a mixture of many aspects of practical and
theoretical computer science, offering solutions to many common tasks, PHP's libraries
show a definite bias towards web-frameworks and content management systems.
Attempts to extend PHP's reach to desktop applications through GUI libraries seem to
have died down and become neglected, while the web-frameworks and content
management systems are still under active use and development.

4. Conclusion

In conclusion it seems that both programming languages have a right to exist.
Despite lacking standardisation and being limited in use beyond as a server-side
scripting language, PHP offers a quick and easy means for Java or C# programmers to
transition to PHP, if that should be necessary for a project. Furthermore, the vast amount
of content management systems and web-frameworks written in PHP make it a good
language of choice for programmers wanting to learn a web programming language. Its
easy and lenient nature facilitate the writing of extensions for them. This can, however,
be deceptive, as its weak typing system doesn't always produce errors when they would
occur in stronger typed languages, thus creating an error source that might not always be
discovered. The fact that PHP only has limited support for threading and that the garbage
collector cannot be tweaked easily make it less suitable for long running applications,
such as programs with GUIs or servers. It seems it is best placed to respond to requests
on servers, in scripts that run for a short time and terminate, which also seems to be for
which PHP is most commonly used.
Python is a more versatile language, as it wasn't designed for any other specific purpose
than to be versatile. It seems the inclusion of a formal language description into Pythons
documentation nourished the creation of alternative implementations and the academic
use of the language, as is evident through the creation of implementations such as PyPy,
that take a very different approach to execution than the traditional CPython, as well as

                                           15
the creation of a large number of scientific Python modules. Its versatility is evident
through the number of different modules for various different purposes that were written
for Python, as well as through the means parts of the language, such as class creation and
destruction, the garbage collector or member access, can be modified to suit the needs of
a given situation or problem. As such, it is hard to name usage scenarios for the Python
language, as there is very little it cannot be used for.

The following table offers an overview of the previously discussed topics:

                                Python                               PHP
Language
Documentation      Separate documentation for each    A single documentation for all
                   release based on the previous      versions. Major parts of the Zend
                   release. The grammar is            Engines internals are missing.
                   included in a BNF-like notation.   PHPs documentation lacks a
                                                      formal grammar description.
Syntax             Uses mandatory indentation for Uses      C-like   brackets   for
                   code structuring.              structuring code. Indentation is
                                                  optional. Inconsistencies in case
                                                  sensitivity.
Implementations CPython as the standard               The standard implementation for
                implementation written in C.          PHP comes in form of the Zend
                Major                alternative      Engine 2, which was written in C.
                implementations include Jython        Probably due to the lack of a
                written in Java and IronPython        formal grammar description, no
                written in C#. Further, more          other complete implementations of
                specialized     implementations       PHP exist.
                exist.
Garbage            Reference counting with cycle      Reference counting with cycle
Collection         detection. The garbage collector   detection. The garbage collector
                   can be finely tweaked and          can only be tweaked through
                   analyzed at runtime.               altering the PHP source.
Type System        Strongly typed, duck-typing        Weakly typed
Extensibility      Natively through a C API as Natively through a C API. This
                   well as through a system to call feature isn't well documented.
                   C code from within Python
Embeddability      Possibility to send Python code Undocumented feature. An embed
                   directly to the interpreter, as well SAPI is contained in the PHP

                                           16
as through a C API                   source, but is experimental.
Paradigms
Object         Classes with private and public      Classes with private, protected and
Orientation    for    encapsulation,     multiple   public for encapsulation, single
               inheritance, fine tuning of access   inheritance, abstract classes and
               and class creation through           interfaces. Exception handling is
               special methods and meta             inconsistent with some runtime
               classes. Exception handling is       errors that don't throw catchable
               consistent for runtime errors.       exceptions.
Functional     Anonymous functions through          Full support for anonymous
               lambdas, restricted to contain       functions including expressions
               only expressions. Functions can      and statements. Functions can be
               be passed by name.                   passed anonymously or by name.
Concurrency    Available in CPython via Only available on Unix systems
               multiprocessing, also via a fake by means of multiprocessing.
               threading                interface.
               Implementations that support
               true threading are available.
Uses
Web Servers    Specification for Web Server Through SAPIs created for a range
               Gateway Interface supported by of webservers, most commonly
               many different webservers.     Apache.
CLI            Out of the box support with an       CLI support is built by default
               interactive shell being the          when compiling PHP, it requires
               default action for the python        extra switches for an interactive
               executable.                          shell.
Standalone     Various GUI toolkits are GUI toolkits for PHP are available
Applications   available for GTK, Qt, Tcl/Tk. for Qt and GTK, but these appear
                                              to be outdated.
Libraries      Large range of libraries for PHP libraries are largely restricted
               many different subjects, such as to CMS and web-frameworks and
               scientific calculations, statistics, tools for these.
               graphics,       games,        web-
               frameworks.

                                        17
5. References

[PSF2011-1]     Python Software Foundation, Python v3.2 documentation, Language
                Reference, Introduction, 2011,
                
                (13.06.2011)
[PSF2011-2]     Python Software Foundation, Python v3.2 documentation, Language
                Reference, Lexical Analysis, Indentation, 2011
                 (13.06.2011)
[PSF2011-3]     Python Software Foundation, Python v3.2 documentation, Language
                Reference, Lexical Analysis, Keywords, 2011
                 (13.06.2011)
[PSF2011-4]     Python Software Foundation, Python v3.2 documentation, Extending
                and Embedding, Reference Counts, 2011
                 (13.06.2011)
[PSF2011-5]     Python Software Foundation, Python v3.2 documentation, The Python
                Standard Library, Python Runtime Services, Garbage Collector
                Interface, 2011
                 (13.06.2011)
[PSF2011-6]     Python Software Foundation, Python v3.2 documentation, The Python
                Standard Library, Built-in Functions, 2011
                 (14.06.2011)
[PSF2011-7]     Python Software Foundation, Python v3.2 documentation, Glossary,
                2011
                 (14.06.2011)
[PSF2011-8]     Python Software Foundation, Python v3.2 documentation, The Python
                Standard Library, Built-in Exceptions, 2011
                 (14.06.2011)
[PSF2011-9]     Python Software Foundation, Python v3.2 documentation, The Python
                Standard Library, Generic Operating System Services, ctypes, 2011
                 (14.06.2011)
[PSF2011-10]    Python Software Foundation, Python v3.2 documentation, Extending
                and Embedding the Python Interpreter, 2011
                 (14.06.2011)
[PSF2011-11]    Python Software Foundation, Python v3.2 documentation, Extending
                and Embedding the Python Interpreter, Embedding Python in Another
                Application, 2011
                
                (14.06.2011)

                                        18
[PSF2011-12]   Python Software Foundation, Python v3.2 documentation, The Python
               Tutorial, Classes, 2011
                (14.06.2011)
[PSF2011-13]   Python Software Foundation, Python v3.2 documentation, Language
               Reference, Data Model, 2011
                (14.06.2011)
[PSF2011-14]   Python Software Foundation, Python v3.2 documentation, Language
               Reference, Simple Statements, Assignment Statements, 2011
                (14.06.2011)
[PSF2011-15]   Python Software Foundation, Python v3.2 documentation, The Python
               Standard Library, Abstract Base Classes, 2011
                (14.06.2011)
[PSF2011-16]    Python Software Foundation, Python v3.2 documentation, Language
               Reference, Expressions, 2011
               
               (14.06.2011)
[PSF2011-17]   Python Software Foundation, Python v3.2 documentation, Python
               Frequently Asked Questions, Design and History FAQ, 2011
                (14.06.2011)
[PSF2011-18]   Python Software Foundation, Python v3.2 documentation, The Python
               Standard Library, Optional Operating System Services, threading, 2011
                (14.06.2011)
[PSF2011-19]   Python Software Foundation, Python v3.2 documentation, The Python
               Standard Library, Optional Operating System Services,
               multiprocessing, 2011
               
               (14.06.2011)
[PSF2011-20]   Python Software Foundation, Python v3.2 documentation, The Python
               Standard Library, Internet Protocols and Support, 2011
                (14.06.2011)
[PSF2011-21]   Python Software Foundation, Python v3.2 documentation, The Python
               Tutorial, Using the Python Interpreter, 2011
                (14.06.2011)
[PSF2011-22]   Python Software Foundation, Python v3.2 documentation, The Python
               Tutorial, An Informal Introduction to Python, 2011
                (14.06.2011)
[PSF2011-23]   Python Software Foundation, Python v3.2 documentation, The Python
               Standard Library, Graphical User Interfaces with Tk, IDLE, 2011
                (14.06.2011)
[PSF2011-24]   Python Software Foundation, Python v3.2 documentation, Python
               Frequently Asked Questions, 2011
                (14.06.2011)
[PSF2011-25]   Python Software Foundation, Python v3.2 documentation, Language

                                       19
Reference, Execution Model, 2011
               
               (13.06.2011)
[PSF2011-26]   Python Software Foundation, Python v3.2 documentation, The Python
               Tutorial, Errors and Exception, 2011
                (13.06.2011)
[PW2011]       wiki.python.org, Python 2 or Python 3, 2011
                (14.06.2011)
[PSF2011-25]   Python Software Foundation, Python v3.2 documentation, History and
               License, 2011
               
               (14.06.2011)
[PSF2009]      Python Software Foundation, Mission Statement of the Python
               Software Foundation, 2009
                (14.06.2011)
[PE2003]       Phillip J. Eby: Python Web Server Gateway Interface v1.0, Python
               Enhancement Proposal, 2003-2011,
                (14.06.2011)
[PE2010]       Phillip J. Eby: Python Web Server Gateway Interface v1.0.1, Python
               Enhancement Proposal, 2010-2011,
                (14.06.2011)
[WSGI2010]     wsgi.org, What is WSGI, 2010,
                (14.06.2011)
[WSGI2011]     wsgi.org, Frameworks, 2011,
                (14.06.2011)
[GD2011]       Graham Dumpleton, What is mod_wsgi, 2011
                (14.06.2011)
[AS2011]       The Apache Software Foundation, Apache Attic, Apache Quetzalcoatl,
                (14.06.2011)
[AS2010]       The Apache Software Foundation, Mod_Python, 2010
                (14.06.2011)
[SPY2011-1]    SciPy.org, Homepage, 2011,
                (14.06.2011)
[SPY2011-2]    SciPy.org, NumPy and SciPy Documentation, Reference, 2011
                (14.06.2011)
[KH2007]       Konrad Hinsen, Scientific Python, Overview 2007
                (14.06.2011)
[RPY2010]      Rpy.sourceforge.net, About, 2010
                (14.06.2011)
[RP]           The R Project for Statistical Computing,
                (14.06.2011)
[PG]           PyGame, Wiki, About,
                (14.06.2011)
[PO]           Python-Ogre, Homepage,

                                       20
(14.06.2011)
[PGL]         PyOpenGL, Homepage,
               (14.06.2011)
[DP2011]      Django Software Foundation, Homepage, 2011,
               (14.06.2011)
[AC2011]      Agendaless Consulting, Pylons Project FAQ, 2011,
               (14.06.2011)
[PP]          PylonsProject.org, About Pyramid,
               (14.06.2011)
[IP2011]      Microsoft Corporation, Iron Python, Overview,
               (13.06.2011)
[PP2011-1]    The PyPy Project, PyPy, Features
               (13.06.2011)
[PP2011-2]    The PyPy Project PyPy v1.5 Documentation, Coding Guide, RPython,
              2011
              
              (13.06.2011)
[SL2011-1]    Stackless, Homepage,  (13.06.2011)
[PyGTK2010]   The GNOME Project and PyGTK Team, PyGTK Homepage, 2010
               (14.06.2011)
[RBC2010]     Riverbank Computing Limited, PyQt Homepage, 2010
              
              (14.06.2011)
[NOK2009]     Nokia Corporation, PySide FAQ, 2009,
               (14.06.2011)
[TKI2011]     Tkinter Wiki, Front Page, 2011,
               (14.06.2011)
[PHP2011-1]   The PHP Group, PHP Manual, About the manual, 2011
               (13.06.2011)
[PHP2011-2]   The PHP Group, PHP Manual, PHP at the Core: A Hacker's Guide to
              the Zend Engine, 2011
               (13.06.2011)
[PHP2011-3]   The PHP Group, PHP Manual, User Note System, 2011
               (13.06.2011)
[PHP2011-4]   The PHP Group, PHP Manual, Escaping from HTML, 2011
              
              (13.06.2011)
[PHP2011-5]    The PHP Group, PHP Manual, Preface, 2011
               (13.06.2011)
[PHP2011-6]   The PHP Group, PHP Manual, FAQ, General Information, 2011
               (13.06.2011)
[PHP2011-7]   The PHP Group, PHP Manual, Function Reference, PHP Options/Info
              Functions, 2011
              
              (13.06.2011)

                                      21
[PHP2011-8]    The PHP Group, PHP Manual, Features, Garbage Collection, 2011
                (13.06.2011)
[PHP2011-9]    The PHP Group, PHP Manual, Language Reference, Types, Type
               Juggling, 2011
               
               (13.06.2011)
[PHP2011-10]   The PHP Group, PHP Manual, Language Reference, Types, Booleans,
               2011
                (13.06.2011)
[PHP2011-11]   The PHP Group, PHP Manual, Language Reference, Classes and
               Objects, Visibility, 2011
               
               (14.06.2011)
[PHP2011-12]   The PHP Group, PHP Manual, Language Reference, Classes and
               Objects, Properties, 2011
               
               (14.06.2011)
[PHP2011-13]   The PHP Group, PHP Manual, Language Reference, Classes and
               Objects, Overloading, 2011
               
               (14.06.2011)
[PHP2011-14]   The PHP Group, PHP Manual, Language Reference, Classes and
               Objects, Inheritance, 2011
               
               (14.06.2011)
[PHP2011-15]   The PHP Group, PHP Manual, Appendices, Classes and Objects,
               extends, 2011
                (14.06.2011)
[PHP2011-16]   The PHP Group, PHP Manual, Language Reference, Classes and
               Objects, Class Abstraction, 2011
               
               (14.06.2011)
[PHP2011-17]   The PHP Group, PHP Manual, Language Reference, Classes and
               Objects, Object Interfaces, 2011
               
               (14.06.2011)
[PHP2011-18]   The PHP Group, PHP Manual, Language Reference, Functions,
               Anonymous Functions, 2011
               
               (14.06.2011)
[PHP2011-19]   The PHP Group, PHP Manual, Function Reference, Process Control
               Extensions, PCNTL, Installation, 2011
                (14.06.2011)
[PHP2011-20]   The PHP Group, PHP Manual, PHP at the Core: A Hacker's Guide to

                                       22
You can also read