decorative image for blog comparing php vs python
March 27, 2024

PHP vs. Python: How to Pick the Right Option for Your Web App

PHP Development

Comparing PHP vs. Python is a common exercise for teams building new web applications, or modernizing old ones. However, with both languages offering a unique set of benefits, deciding between the two can be difficult.

In this blog, I give an overview of some of the key similarities and differences when comparing PHP vs. Python, including overall popularity, security, performance, and other notable factors you will want to consider before making your decision. Let's dive in!

Back to top

PHP vs. Python: Overview

Choosing between PHP vs. Python requires weighing a number of factors, including performance, ecosystem, security, and more. But before we dive in on their key similarities and differences, let's start with the basics.

What Is PHP?

PHP is a web scripting language with its roots firmly planted in the early days of the web as a way to glue data into HTML pages. 

While PHP, the web, and HTML, have grown a lot since its inception in 1994. PHP as a language has remained true to its heritage and continues to be a dominant force in the world of web development, with well over 3/4 of the known web reporting in as PHP pages

The many frameworks and open source projects based upon PHP like Laravel and WordPress have propelled PHP into the stratosphere of web development languages, pretty much eclipsing nearly all server side scripting alternatives.

If you're looking for more information on PHP, our blog What Is PHP? gives a good overview of the history and use common use cases for PHP.

What Is Python?

Python is a general purpose scripting language created because its author did not like PERL and also because he believes that “Code is read much more often than it’s written."

After some years of use as kind of the “glue” of open source, Python has recently excelled as a data analytics and machine learning language thanks to its many libraries. It is also wildly popular in DevOps and boasts many projects in this space. Python is generally at or near the top of almost every language ranking thanks to its libraries in AI and machine learning space.

Back to top

PHP vs. Python: Key Similarities

There are a few similarities between PHP and Python, but the primary similarity is that they are both wildly popular scripting languages, which means they are not pre-compiled. The old perception of slowness in scripting is pretty much all gone these days as the processors and optimizations of each language have rendered this lag almost inconsequential. 

The recent RedMonk report that analyzes two massive data sets (stack overflow and GitHub) put these two in the top 5 programming languages. These languages were both originally created by a single person (Guido Von Rossum for Python and Rasmus Lerdorf for PHP) and have grown to be governed by committees. 

Back to top

PHP vs. Python: Key Differences

PHP and Python have a number of differences, but their biggest differences lie primarily in their use cases, and in curly braces. 

PHP is built for web scripting and has, for the most part, stuck to its knitting on that purpose. With that said you can do batch work and command line instructions, but there isn’t much of a desire for that as PHP tends to still be the easy way to get data out to a web page. Python, being more general purpose, means it can be a good fit for anything — but may lack specialization in the core of the language. In fact, how these two languages operate is almost diametrically opposed. 

The core of PHP has well over 1,000 functions built-in while the Python core contains about 70. 

This makes sense since PHP’s sole purpose is to produce sub second response time on web pages so it would want a lot of tools immediately at its disposal. While Python tends to be more thrifty and asks the developer to bring what it needs when it needs it. Both languages sport a very powerful and diverse built-in libraries.

The curly brace discussion is significant because the author of Python wanted the language to be easily read. As a result, Python focused on indentation to set scope. This is in contrast to PHP, which leverages the “C -Derivative” of curly braces for managing the purview of variables, classes and more. 

For folks new to programming, this makes Python attractive as it keeps the language concise and appears a bit less cluttered. But for those with a programming background, some find the lack of explicit scope definition a challenge.

Libraries

These primary differences lead us to a discussion about the modularity of the languages, which in turn necessitates a discussion of the communities. But modules are HUGE. 

In the PHP landscape there are fundamentally two ways new processes are enabled: Extensions and Packages. The difference lies in that most extensions are written in C while packages are typically written in PHP and installed via Composer. The extensions are designed to run in memory with PHP while packages are more of an extension to the customers code base and user maintainable. 

In Python the module landscape is a tad simper as most everything is a module that is installed via PIP.

Community and Ecosystem

I love it when students tell me they want use a new language like Python instead of PHP. Then I immediately reply, “you know Python is actually older than PHP, right?” 

Both technologies purport a lively community and both are relatively safe from harm. 

While Python lost its benevolent dictator for life (BDFL) in Guido Von Rossum, it did gain a committee that steers the language. On the other end, the recently-created PHP Foundation helps augment PHP development by directly paying for developers to work on the PHP language. (It's also worth noting that Zend is a founding member of The PHP Foundation.)

Performance

Performance is always an interesting discussion. Which one is faster? My immediate response to this question is “to do what?” 

Let’s break performance down into three categories: Developer, web app, data manipulation. You see, I grew up in the 80’s and 90’s when performance basically had nothing to do with the developer and everything to do with saving 1 instruction from an algorithm because developers were cheap and MIPS were expensive. Could I get by with Fortran here or did I have to delve back to Assembly for the I/O on my monolithic COBOL applications to squeeze out one more second of response time? 

Somewhere after the turn of the century, and thanks to Moore’s Law, the coin flipped — and developers were suddenly more expensive than compute power. Gone were the days of everyone being transfixed on every little bit or byte in a process word. We had interactive compiling and a lot of advances designed to get the developer to be more productive and I think that is a key metric of performance. 

I’m not saying that a developer should focus on cranking out the worst code in exchange for speed of development but very few website truly need brutal scalability. For those we need to aggressively focus on architecting performance and horizontal scaling. For the rest, let’s make sure the developer can be as productive in building the applications that will ensure competitive advantage.

Both Python and PHP are scripting languages and both have revised their approach to parsing, caching and threading over the many years. As I write this, the Python development team has addressed one of the main performance bottlenecks of Python by making the GIL optional. Anyone who has investigated the GIL or the Global Interpreter Lock understands that Python can only do one thing at a time. 

For PHP, all you need to do is look back to the PHP 5 to PHP 7 revolution. So many things went into creating arguably the best performance improvement in the language not to mention memory optimization which affects horizontal scaling in the virtual server world incredibly. 

For a real world case study on the benefits of updating your PHP stack, check out this case study with Badoo

If you are looking for raw performance on the web, PHP’s experience and architecture give it a notable advantage. 

For data manipulation, PHP is no slouch but the Python libraries available for Pandas, Data Frames and such are numerous and well supported. 

Again, we are back to a discussion about use cases. Honestly, if I am building a business intelligence layer with heavy analytics in Python I would probably drive the presentation of it with PHP. While Python can certainly do web work, the ease of building that with PHP and the overlapping support for data access make it a much better solution.

Learning Curve

This is probably the most controversial subject I’ll cover. I wish there was a clear winner on this, and of course the respective communities will always tell you their language is the easier language to learn and use. But as we’ve seen throughouht this comparison, with any point there are pros and cons. 

PHP was originally envisioned as an “easy way to get data to the web”. And this is still true. But being an “easy” solution means that sometimes folks are building websites who maybe don’t know or don’t care about standards, security, and other issues that could come back to haunt them.

As a result, the PHP community has spent countless hours enhancing the language and standards to ensure that best practices are implemented at the core of the language as much as possible. These standards, while important, can lead to backward compatibility breaks or complex structures. So, the learning curve to get proficient in PHP has ticked up a bit, but for good reason. 

The Python community is quick to say that Python is an easy language to learn, and, for beginners doing basic tasks, that is true. I think the lack of curly braces syntax for setting scope is great for non-developers to come on board to the language. But inside the Python landscape lurks a more sinister side where some basic concepts are not as strictly enforced by the language. 

A good example is private properties in an object, which are strictly enforced in PHP but technically do not exist in Python. I’ve seen various textbooks offer differing approaches for this, ranging from __(dunders), to single underscores, to completely ignoring it. These conflicting messages about how to code a private property which may lead to confusion at the least or insecure code at the worst. Is being too flexible a shortcoming? I’ll let you be the judge.

Security

I wish security was a discussion solely around the language. But when working with the web, there are a number of factors you need to take into consideration, including things like firewalls, Apache or NGINX configs, load balancing, etc. That aside, I’m going to try to keep this focused strictly on the languages. 

First and foremost is maintenance and both PHP and Python have very active communities supporting the languages. PHP’s support matrix illustrates community based support for bug fixes at 2 years per release and 3 years for CVEs. Zend, of course, extends that support window by at least 2 more years for CVEs. 

For Python, the community support for a given release is about 1.5 years for bug fixes and a total of 5 years for security fixes. As you can see Zend brings the update components in parity with the Python community.

I have touched on pieces of security throughout this article regarding the language itself, but keep in mind this is a highly subjective point as the philosophies and use cases differ widely. I would argue that PHP’s strict object model does protect data more aggressively and can lend itself to better practices. Also, the efforts of the PHP community to lock down the language and implement better practices is admirable. For that security, however, some ease of use had to be sacrificed. 

As these are both very active open source projects, which ultimately means any comparison on security is a draw. This is because the ultimate responsibility for language security resides with the development team keeping current with the releases and best practices via PHP Standard Recommendation (PSR) or Python Enhancement Proposal (PEP) 8.

Development Landscape

According to DistantJob, there are about 10.1 million Python developers and about 6.3 million PHP developers. 

If your reaction to those numbers is “Oh my! We need to switch to Python immediately because there are more developers," don't be so hasty. In most cases, a PHP developer knows PHP, and other common web languages like HTML, JavaScript, etc. But it's also probable that many PHP developers also know Python and many other technologies. 

Similarly for Python, I have seen very few jobs that are for “Python” developers. In many cases, Python developer is also a Data Analyst, Dev Ops Architect, training machine learning models, etc. My point is that I believe the number of folks who claim to be Python developers is skewed. This is likely to be true since, for many of them, the actual Python development aspect of the role is secondary to another responsibility for the job. 

It’s not unlike many charts I see where they add languages that nearly everyone uses (like CSS and SQL), where, if they're not near the top of the charts, it leads to me questioning their methods in building their rankings.

Code Styling PEP 8 and PHP FIG

Both PHP and Python have active communities that help to standardize their respective code bases, and that's a good thing. In today’s environments, many developers are frequently expected to transition from one language to another. But just transitioning from one developer’s style to another in the same language can be daunting. These solutions are designed to help ease that transition among developers, projects, libraries, etc. These guidelines offer a standard approach to variable naming, spacing and so much more. 

In the Python world we have the PEP. PEP 8, authored by Guido Von Rossum himself, is designed as a guideline more than a law. Several IDE’s and linters will help developers enforce these guidelines and offer options to adjust. 

In the PHP space we have the PHP Framework Interop Group, or PHP-FIG, which was born out of a similar need but for a slightly different reason. The PHP-FIG offers an optional standard that can be enforced similarly to the Python PEP 8.

Back to top

Final Thoughts

As you have probably gathered through the course of this article, there is no “magic” answer for when to choose Python vs. PHP. Choosing between the two options ultimately boils down to your use case. 

While there are libraries available to do web development in Python, I still feel it’s a bit like reaching around your back to scratch your elbow. So, I’d give the nod to the web to PHP, APIs and more obviously. 

But for batch work ETL, ML, DA or for infrastructure, there is a wide range of Python code and libraries for handling a lot of what goes on behind the scenes in the data center. 

At the end of the day, you are much better off learning and leveraging both for the appropriate use cases. Right hammer on the right nail and all that.

Need Help Modernizing Your PHP App?

Zend can help! With solutions that include LTS for EOL PHP versions and migration/modernization services that can keep your app secure and performant. Learn more about what we offer via the links below.

SEE PHP LTS OPTIONS EXPLORE MIGRATION SERVICES

Additional Resources

Back to top