[Web-SIG] Useful ideas from PHP
Simon Willison
cs1spw at bath.ac.uk
Fri Oct 17 11:56:21 EDT 2003
I've been working with PHP for several years, but have recently started
to make the switch to Python for web development. There follow some
thoughts on PHP's web development capabilities compared to Python's. PHP
has a number of tricks that are worth borrowing for the Python standard
library - although in my opinion the ability to embed code in HTML is
not one of them.
Things PHP does better than Python
==================================
* $_GET, $_POST, $_COOKIE, $_FILES, $_REQUEST, $_SERVER, $_ENV
http://www.php.net/manual/en/language.variables.predefined.php
These global dictionaries provide immediate access to information sent
from the client The first three provide information from teh query
string, posted forms and cookies respectively. $_FILES handles uploaded
files, $_REQUEST allows access to data regardless of where it came from
(like Python's FieldStorage() module does at the moment) and $_SERVER
and $_ENV are server and environment variables.
This is an improvement on Python because these arrays are consistent.
Everything is available in a straight forward dictionary (no
fields['name'].value oddness), there's no need to explicitly parse
cookies from their environment variable and it's possible to tell the
difference between POST and GET data while retaining the convenience of
just being able to get the data without caring about the method used to
send it.
* header(), setcookie()
http://www.php.net/manual/en/features.cookies.php
These functions allow a user to manipulate the headers being sent back
to the user and provide an easy method for setting cookies. In Python
CGIs you have to manually ensure you send the headers before any HTML by
being careful with your print statements. Some kind of abstraction for
headers is a good idea.
* Native session support with session_register and $_SESSION
http://www.php.net/manual/en/ref.session.php
This is a pretty useful feature in PHP, which could be easily replicated
in Python. It would probably be better as a separate session module
rather than adding it straight in to the CGI module.
Things Python does better than PHP
==================================
Pretty much everything else. Python's syntax and semantics are cleaner,
the language is more powerful and expressive and the standard library
for the most part is outstanding. Python's database access is cleaner as
well. If Python only had a cleaner CGI API and a more widely available
Apache module it could make serious inroads in to PHP's market share.
Things PHP has that Python doesn't need
=======================================
A big fuss is always made of PHP's ability to embed code straight in to
HTML, but in practise most experienced PHP developers tend to avoid this
feature and use some kind of templating system instead, preferring to
separate their application logic and presentation logic. Python is
already well served by a number of excellent template libraries such as
Cheetah.
Cheers,
Simon Willison
http://simon.incutio.com/
More information about the Web-SIG
mailing list