PHP and Python both are dynamic typed language, so there is no point the discuss anything related in this aspect.
Exception is first class citizen
1 2 3 4 |
try: a = 1 / 0 except: print 'Caught' |
Prints 'Caught'
Let's look at PHP:
1 2 3 4 5 6 |
try{ $a = 1 / 0; }catch (Exception $e){ print "Caught"; } I cannot believe by default this still gives me PHP Warning: Division by zero |
Better functions
Imagine you can write functions like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
def marry(man, woman): ... marry('John', 'Julie') marry(woman='Julie', man='John') def party(*args): for person in args: print person party('John') party('John', 'Ben', ...) def save(name, *kwargs): data = {'name': name} data.update(kwargs) write_to_db(data) save('John') save('John', gender='male', birthday='01/01/1970') |
Short-lived vs Long-lived
Almost all PHP scripts are short-lived, including most frameworks, which means to serve a request, your PHP application will run from start to finish. However, Python can run as processes to serve requests, for example, A Django application runs as a process, so initiation code only runs once, serve a request, then wait for the next request to serve.
Imports Properly
Since 5.3, PHP introduced namespace, since they used ."dot" for concatenation, so they had to use the \"slash", looks like this
1 |
namespace NZ\NET\LIN\Test |
And use like this
1 |
use NZ\NET\LIN\Test |
But it doesn't actually import the Test class! Unless you use __autoload!