Python & Django for PHP Refugees

Jamie Matthews

Brighton & Hove Python User Group

12th April 2011

About me

  • Used both Python and PHP (amongst other things) for ~8 years, on and off.
  • Web freelancing using PHP while at uni.
  • Python for MSc work: genetic algorithms, gluing together C code.
  • ~3 years full-time PHP developer.
  • Co-founded DabApps, using Python and Django.

Tesco Cars


  • PHP ain't so bad
  • PHP and Python: compare and contrast
  • PHP and Django: views, templates, models, other goodies
  • Demo time
  • Homework


PHP ain't so bad

  • PHP is a programming language and a web framework and a templating language.
  • PHP is always going to be easier to deploy than Python (thanks to mod_php).
  • It is perfectly possible to write good code in PHP. You just have to be very careful.

For beginners

  • Python is easier to learn than PHP.
  • Web programming with PHP is easier to learn than Python and Django.
  • ...but, web programming isn't meant to be easy. Web applications are complex.
  • Most important: get your head around the concepts. The details come with experience.


  • Both are interpreted, high-level languages with dynamic typing.
  • Both are multiparadigm languages. Support procedural, object-oriented and functional styles.


1 <?php
2 items = array('spam', 'eggs', 'beans');
3 foreach ($items as $item) {
4     echo $item . "\n";
5 }
6 ?>


1 items = ['spam', 'eggs', 'beans']
2 for item in items:
3     print item
  • WTF? No semicolons? No curly braces?
  • OMG! Significant whitespace!

Conventions & Consistency


  • CamelCase
  • lowercase_with_underscores
  • something_Else_Weird($MY_VAR);
  • Intentation: tabs? Two spaces? Four spaces?


PEP8: Style Guide for Python Code

No arguments.


PHP < 5.3

...what namespaces?

Everything is global. The only way to namespace code is to use a class.

PHP >=5.3

Namespaces are optional. No namespace declaration? No namespace.


Namespaces are one honking great idea -- let's do more of those!

A file containing Python code (a module) gets its own namespace.

1 some_variable = "Hello Brighton"

1 from firstfile import some_variable
2 print some_variable

Everything is an object

Strings are objects

1 my_string = 'hello'
2 print my_string
3 print my_string.upper()
4 print 'hello'.upper()

Functions are objects

1 def say_hello(name):
2     print 'hello ' + name
4 def say_goodbye(name):
5     print 'goodbye ' + name
7 greetings = [hello, goodbye]
8 for greeting in greetings:
9     print greeting('brighton')

Classes are objects

... but let's not go there.

Python global built-in functions: 74

Python built-in functions

PHP global built-in functions

PHP built-in functions

PHP global built-in functions (2)