There are dozens of great languages to learn. Today we’ll be breaking down the differences between two of the best: PHP vs Python. Which one is best for your application? Which is going to give you the best return on your development time?
We’ll pit these two head-to-head and find out which is the best choice for you.
Ease of Installation
If you’re building a small application by yourself, PHP has three simple advantages.
Installing Python can be a difficult proposition. If you’re on OS X, there’s an existing system version of Python that came installed on your computer.
It’s outdated and not suitable for application development.
Trust me, you don’t want to install new packages on the system Python. You’ll need to install a new version to make sure your system is as stable as possible.
Python on Windows can also take a bit of work to install.
The most common advice is to use a Windows package manager like Chocolatey as you start out. If you’re planning for a team, that little bit of extra work can add up.
If your team’s on Linux, though, you’ll have no problems installing Python.
PHP wins this round on account of being easier to install pretty much everywhere.
Score: PHP 1, Python 0
Python has a strong advantage over PHP.
Python uses Pip (a recursive acronym that stands for “Pip Installs Python”) to manage packages. Pip makes managing different libraries within a Python application simple, it’s fast, and your project’s requirements are easy to parse at a glance. As a Python developer, Pip is an essential tool to have in your toolbag.
PHP’s library management isn’t as strong.
PHP 7 introduced Composer, which is a fantastic tool, mostly feature complete, and (when paired with a tool like Packagist), you’ll find package management close to on-par with Python’s. Unfortunately, it’s still pretty young.
At the end of the day, Python has a broader variety of mature packages, and their tools are a bit easier to install and use at this time.
One point for Python.
Score: PHP 1, Python 1
If that doesn’t describe you though, being able to segregate your application’s installed language and library versions is a critical part of staying sane as a developer.
Python, again, has a best of breed application to manage environments. Virtualenv is a system used to install multiple versions of Python side by side and switch between them quickly. Virtualenv is one of the original environment managers and has been actively maintained for more than a decade. It’s a terrific piece of software.
PHP isn’t so lucky. There is an analog for virtualenv for PHPcalled virtPHP,. but that project has been archived and is no longer actively maintained. Their own developers actually suggest using containers instead.
This is wise advice as containers have all sorts of advantages for developers and operational deployment. Unfortunately, they’re not a great fit for every environment. If your environment is one of those, PHP is at a disadvantage here.
Python scores another point.
Score: PHP 1, Python 2
Easy to Learn
Python and PHP are very similar. Both are object-oriented interpreted languages that run on a variety of operating systems. They’re both dynamically typed and have terrific IDE support. Lastly, Python and PHP both stand out in the world of syntax.
PHP requires that you prefix all variables with a $ sign, and uses the -> operator to indicate a method called on an object. Those make it stand out from other C-style languages.
Python’s syntax is dissimilar from many other languages. It disposes of curly braces and instead it embraces whitespace sensitivity.
So, which is easier to learn?
That’s tough to say.
PHP has some advantages. PHP has a double-edged sword in their array concept, which serves as something like a combination between Python’s list and dictionary data types. It’s a benefit because if you understand how to use an array, you can do a lot in PHP. However, it’s also a negative because PHP often lacks quality implementations of other more focused data types.
If you’re trying to do something that doesn’t fit into an array, using PHP means you might need to write your own data type.
In Python, that’s not the case.
Python has a wide variety of built-in data types that are ready to go right out of the box. The challenge with Python is often knowing which tool to use for the specific job that you’re doing. That’s still a challenge, but it’s usually an easier one to solve. Python does have a disadvantage that the Python 2 to Python 3 transition has not been backward compatible.
One more quick word about learning PHP: it’s an old language. That means there are a lot of outdated, and frankly bad, tutorials out there. That’s a real impediment to learning the language from scratch.
Despite that, both score in this category.
Score: PHP 2, Python 3
Many of the things that make web applications slow aren’t related to programming languages at all. Slow database queries bog down every programming language. Relying on excessive network queries or reading a lot of information from a disk will slow you down every time. That said, in most circumstances, PHP is a faster programming language than Python.
The core PHP team has done a great deal to speed up PHP, and with PHP 7, they succeeded.
That evaluation doesn’t come without caveats though. PHP famously restarts your entire application every time a web request starts. For the most part, that’s not a problem. However, it can be trouble if you’re working with an application that needs to share resources across requests.
Because PHP sets up and tears down the entire application on every request, sharing resources can be difficult. That’s not as much of a problem for Python, and in fairness, it’s something that most web applications don’t need to do.
It’s also important to remember that no matter the language, you’re not guaranteed a fast web app. If you’re building a new web application and you’re looking for the best performance, you should plug into a tool like Retrace. It’ll help you identify problem spots within your application and speed them up no matter which language you choose.
PHP gets the point here.
Score: PHP 3, Python 3
When you’re building a new application, the most important question isn’t always what happens when things work well. Often, you need to know what happens when things break. For experienced developers, that means attaching a debugger and figuring out where your code went wrong.
Both PHP and Python feature terrific debuggers which thousands of developers use daily. Python’s step-through debuggers integrate directly into IDEs like Visual Studio Code or PyCharm (my personal favorite). If you’re writing Python, all you have to do is push the “debug” button and you’re off to the races.
In PHP, it’s not always quite so simple. Sometimes, debugging PHP can require a little bit of setup work first. It’s not a big deal to get a PHP debugger up and running. Anyone can do it. However, it’s good to know going in.
Debugging isn’t just for development, though. A free trial of Retrace will bring powerful error monitoring and code improvement to your production apps, too.
So, which language scores here? I would say that they tie. Both bring powerful, first-class debuggers with excellent IDE integration.
Score: PHP 4, Python 4
For our final category, we’ll look at language documentation. When you’re diving into a new project, good documentation is critical to your success.
PHP has a robust documentation site with a unique feature. Developers can contribute comments on each documentation page. The documentation already effectively cover all of the built-in features of the language in simple, plain language. The comments can be a cherry on top that help explain complicated concepts from a different direction.
There’s one big drawback, though. Those comments stick around forever. This means that when you’re reading PHP’s documentation pages, the comments that show up first are from very old versions of the language. Sometimes, you’ll be reading a comment left on a function’s documentation page, to find out that the developers removed the behavior described three years ago. This leads to a lot of confusion, as you can imagine.
Python doesn’t have that problem, because their documentation doesn’t allow comments. Their documentation is written in a less-conversational tone. Some people find this helpful, while others have difficulty understanding. Python’s documentation is also a bit more terse, overall.
Both these languages leverage their documentation well. However, I need to pick a winner. In this case, I say that PHP’s regularly-outdated comments are enough to keep it from scoring a point, here.
Score: PHP 4, Python 5
PHP vs Python Conclusion
Our final evaluation ends with Python holding a very slight edge over PHP. Honestly, that feels about right. Both PHP 7 and Python 3 are excellent choices for building a web application in 2020. But if I had to choose, I would pick Python.
When you’re comparing PHP vs Python, which should you choose? Well, that depends on what you need.
Do you need to share environments on the same PC? Python is almost certainly the right answer. Are there developers on your team who already know some PHP? PHP is probably your best bet! Does your application require great Unicode support? Python would be the way to go.
No matter which way you go, it’s not about PHP vs Python. The key is to get out there and get building. You can’t make a wrong decision here. Best of luck building your new application!
- Ansible vs Jenkins - April 27, 2020
- What Is Test Driven Development? - March 23, 2020
- PHP vs Python: Is There a Clear Choice in 2020? - February 26, 2020
- Rails Geocoder: A Guide to Managing Locations in Your Apps - June 27, 2019
- How to Do Authentication Right With Rails Devise - May 21, 2019