Introduction
We will go over Python As A Replacement For PHP.
Did you know that both languages are very different syntantically but support similar frameworks?
I’ve used both Python and PHP extensively in the past and I’m going to try to give you my point of view on the two languages and whether or not Python can be used as a replacement for PHP.
We will break down this in the following sections:
- If Python can replace a big PHP project in a business environment
- If Python can replace a PHP project in a non-business environment
- What challenges you may face when moving from PHP to Python
- How to overcome some of the challenges moving from PHP to Python
- Some questions for you on whether or not the move from PHP to Python is a good idea
I like both languages as I mentioned earlier and I believe both have a place in my heart but here I’m going to give you perspective on making the decision for yourself and see if you really need to make the transition and at what cost this will come to you.
Can Python Replace PHP
This is the answer we will seek in this document. In this section I’m going to leave you with some questions to ask yourself and from there we can drill down and try to identify if it can for your needs. Since this is not a single answer all it depends on whether or not you are in a business, solo fun project or something else so I’m going to split up the questions in those categories.
Can Python Replace PHP Business Project
We will go over some reasons here on if PHP can replace Python for a business project.
- The cost of replacing from one language to another particularly PHP to Python is extremely high for the following reasons:
- The languages differ on how they handle web stuff
- The libraries used are different and the framework of reference is not the same
- Based on the lines of code you have this may be a major effort to go from PHP to Python
- There’s going to be a need for a new testing framework as they are not compatible (note you can keep your functional higher level testing that uses web tests)
- There is no easy way to port the languages such as pseudo framework interlayers it will need to be a clean rewrite
- Business will require a transition plan and slowly weed off the old methodologies
- Time to do this based on your code this can be a very prolonged period
Can Python Replace PHP Home Project
For a home project things are very similar to above but there’s no need for the following:
- No cost since this is a home project
- No time limit restrictions like a business project and no deadlines
- No constraints or slowly depreciating pipelines, you can do the change all at once
Excluding the list above you will still need to go through the steps and process mentioned to port your project from PHP to Python.
Python As a Replacement For PHP Inside HTML
Python uses a very different way of handling HTML. More particularly there’s a popular solution syntax out there. Lets take a look at a common templating engine provided by the Django project here:
{% if user.is_authenticated %}Hello, {{ user.username }}.{% endif %}
In the example above we are basically printing if a user is authenticated their user name and a hello message. This basically is a unique way of generating HTML from Python code in the Django project which is one of the most popular solutions for Python.
On the other hand lets see how someone would do this in PHP code which is basically inline code that actually runs on the webserver and directly generates HTML code.
<?php $user = get_user_info(); if ($user->is_authenticated){ echo "Hello, $user->username"; } ?>
As you notice above the code is vastly different. Basically in the case of PHP the code is the one that generates the HTML, whereas Python uses a templating engine to generate the HTML for it.
Considering the above I belive the two are vastly different when you considering Python as a replacement for PHP inside HTML. This offcourse means you will need to rewrite a lot of your business logic that generates your HTML code.
Should I Switch From PHP To Python
The reasons to switch from PHP to Python entails on understanding the following things:
- Does your project have the necessary budget for the transition
- Do you have time for the transition
- Can you allocate additional resources to manage this process
- Are you ok with rewriting major parts of your test frameworks
- Get familiar with a new language and training the staff
- Understanding that you used some PHP libraries and Python may not have equivalents (this is not typically the case as Python has a very lush set of libraries to provide to you)
If any of those questions seem difficult for you, you should definitely re-consider moving from PHP to Python.
How To Replace PHP With Python
So if you decided to move ahead of moving from PHP to Python here’s a list of things that you will have to do when you start:
- Make a timeline for your deliverable
- Setup a budget on new resources
- Setup a budget for training existing resources
- Get a migration manager to handle the whole process
- Setup a phased approach on which modules you can replace first and slowly start phasing them out
- Setup a plan B in case things go south, a disaster recovery plan
- Ensure you have a few senior guys that will handle the transition plan and are trained in both languages
- Check on licensing and plan accordingly if they comply with your work
I think the list above summarizes a transition from PHP to Python pretty well but you also need to take into account some more technical requirements that’s listed here:
- Ensuring web API’s are the same in terms of routes and REST compliant if needed
- Rendered HTML follows some templating from PHP generation to Python. This one can be very tricky and may require attention to details as the way the code generates is vastly different.
- Database models translating if there’s no changes
Conclusion
We were able to successfully go over Python As A Replacement For PHP, hopefully I answered any questions you may have had and helped you get started on your quest of selecting the right language for your project.
If you found this useful and you think it may have helped you please drop me a cheer below I would appreciate it.
If you have any questions, comments please post them below or send me a note on my twitter. I check periodically and try to answer them in the priority they come in. Also if you have any corrections please do let me know and I’ll update the article with new updates or mistakes I did.
Which language would you go with?
I personally like Python a lot as I have been using it for the last few years. I think it covers all my needs and checks the boxes for getting stuff done. When it comes to PHP I believe it has aged a lot and I do not see myself going back to it unless there’s some project I want to pick up that uses it.
If you would like to find more articles related to Python and PHP:
Some references for the two projects can be found here: