SNAKE_BYTE #36: Two Unlikely Cousins

Our website has undergone multiple iterations since I started at PokitDok. Originally part of a larger Flask app utilizing Jinja and AngularJS, we eventually switched to Lektor, which was a static-site generator that used Jinja syntax, which I touched on in my last snakebyte.

Since then, we have ported our site into a custom WordPress template with the intention of allowing less tech-savvy users to edit content without the need for Front End intervention or a deploy at all. I've touched a lot of PHP (a recursive acronym for PHP: Hypertext Preprocessor) since this undertaking, and it didn't take long for me to grunt and groan, so I thought it'd be fun to utilize this Snake_Byte to compare PHP and Python, two unlikely cousins.

Python is like a teacher who follows the core curriculum to a T, whereas PHP is a teacher who has been here for years and just wants the week to be over so we can all enjoy the weekend.

Let's compare an if statement.
In PHP, you can do:

or use alternate syntax to return the same result:

In Python, the example would be:

Note: In PHP, you can write if ($truestatement) : return 'hooray'; endif; if you just want to be really gross, which is not allowed within the spaces-matter Python world.

If you're working within a template with PHP, dashing in HTML and pulling in variables, things really get nasty. I feel like I've typed more php tags than there are hairs on my head (and I have all my hair still, despite wanting to pull it out occasionally). I have also noticed a tendency of PHP to silently fail or fail in a way that debugging becomes a much longer affair.

A basic example comparing the two used for front end templating is presented below. The use case is a simple task of rendering a button with a variable for the text with a fallback if not set. Note that the Python example assumes you have the required setup (Lektor/Jinja) and WordPress example assumes you have the required setup to utilize the functions/variables included.

Python Template Syntax (Jinja/Lektor)

Note the lack of colon (different from actual Python)

PHP Template Syntax (WordPress)


In addition to the extra lines injecting the PHP, the more complicated things get for PHP, the more you need to separate out individual parts of your template. Also, as you can see, you have to decide whether it is more appropriate to utilize echo or close your PHP and return your html (you can do either), which is not something you really consider with Jinja.

Lastly, here are some random, but useful functions in both languages:

Python PHP
Today's date as day of week import time
import datetime

print datetime.date.today().strftime("%A")

echo date('l');
uppercase/lowercase conversion 'lorem'.upper()
'LOREM'.lower()
strtoupper('lorem');
strtolower('LOREM');
concatenate '{0}@{1}'.format(hello, world)
(advanced concatenation/string formatting, since Python 2.6)

'hello ' + 'world'
(basic concatenation)
$h = 'Hello, ';
$hw = $h . 'World!';

As you can see, there's not a "winner" or "loser" here per se. PHP can do a lot of things and is actually simpler to render certain things (such as get current day of week). However, overall PHP is an older web language, so it carries a lot of baggage, is much easier to make messy/hard to read, and sometimes silently fails, making debugging a nightmare.

I hope this has been a helpful look into the world of both PHP and Python from the perspective of a Front End Engineer.

php python

About Jonathan Fann

Since October 2014, Jonathan Fann has been part of the Front End team at PokitDok. Prior to this, he worked in various capacities within the healthcare and tech realms. His mission at PokitDok is to help disrupt the healthcare industry with sleek and intuitive front end development. He also is a musician and recording artist who frequently shreds the guitar in miraculous new ways around the office.

View All Posts

Leave a Reply

Your email address will not be published.