PHP developers should try Python.

By | February 21, 2016

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

try:
    a = 1 / 0
except:
    print 'Caught'

Prints ‘Caught’

Let’s look at PHP:

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

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

namespace NZ\NET\LIN\Test

And use like this

use NZ\NET\LIN\Test

But it doesn’t actually import the Test class! Unless you use __autoload!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.