A year ago, I completely switched to Python development. Before that, I wrote mostly in PHP for about 4 years. In the process of work, I constantly compared these 2 languages and now I decided to put it all in one article in order to structure the pros and cons. For you, this article may be useful to figure out which one to choose for your tasks.
Quality of code documentation
The first thing that caught my eye when I switched from php to python was the quality of the code documentation and the type hints in popular libraries.
Let’s take for example the most popular http client libs: guzzle – for php and requests – for python.
Open any class in requests and you will see that almost every public method has a **kwarg parameter, which means some set of named arguments. Not all arguments have a type declaration. And so almost in all . Even native libraries do not use typing (discussion on stackoverflow).
In guzzle, all methods have a clear interface with a description and specified types. In php as a whole, the code description culture is higher. Often you will be able to use either without even opening the documentation, but by looking at the source code. In python, you will almost always have to look at the usage examples in the documentation and only then figure out how to do what you wanted to do.
Python is clearly easier to read. Well-written code will look almost like plain text written in English.
When you switch from php to python, at first it is unusual to take into account the presence of decorators and context managers in the code. I considered it superfluous syntactic sugar. But it turned out to be a matter of habit. If you use them correctly, then the code becomes simpler.
For example, caching the results of a method for 2 minutes through a decorator would look like this:
@cache(ttl=120) def foo(x) -> str: ...
Php, on the other hand, contains less syntactic sugar and implicit constructs. This makes it easier to analyze the code with linters and for auto-suggestions in the IDE (I use phpStorm and pyCharm from Jetbrains).
Usage in web applications
In php, one request is always handled by one process; for these purposes, nginx is often used in conjunction with the fpm server.
Consider the most common php web server operation scheme:
It is good because the process in which the request is processed is completely isolated. You do not have access to objects from another request. php-fpm takes full responsibility for managing the number of simultaneous requests and the number of processes created for this.
There are many more web frameworks in php that allow you to quickly create sites, both simple and highly loaded. At the time of writing, the most popular and known to me are: yii, symphony, slim, laravel. Each of them has either their own ORM managers, or third-party ones with very large support. In python, the list of frameworks comparable in terms of support level is much smaller, these are: tornado, django, flask.
Python uses very different schemas. Often this scheme is nginx -> WSGI -> python app. When using the tornado framework, there is no layer for managing processes at all, all control is taken over by the framework itself. Therefore, I conclude that python gives more options in managing the server.
In python, it is much easier to write, for example, a web socket server, or to develop in a microservice architecture.
Use to develop machine learning utilities and models
Python is the absolute leader in these areas. Almost all self-written tools for devops or parsing are much more convenient and faster to do in python. I think the point is that python allows you to dive into the language very quickly without learning deep syntax and allows you to intuitively understand how to use simple tools. Therefore, it is easier for non-professional developers to use python.
For parsing in python, there is a scrapy library, which is not similar in php. Thanks to this, you can write complex parsers much faster.
Developing machine learning models in php is a perversion in my opinion. Python with sklearn, xgboost, pytorch, tensorflow, keras libraries has gone far ahead of other languages in terms of ML development. Even lower-level libraries such as numpy, scipy allow scientists to do complex calculations without delving into the peculiarities of the language.
For web development, in most cases, php is better because:
it has more convenient frameworks that allow you to quickly solve typical tasks
better level of library documentation
Easier server management scheme
But if you have a complex microservice architecture or you are doing a non-standard service that requires an asynchronous web server, then it is better to use python.
For other tasks, such as: parsing, development of ML models, simple utilities, python is more suitable.
If you do not agree with me, or you have your own observations, write about it in the comments.