Take a look at it from a different perspective
- Have you ever had a hard time finding an SDK or library implementation for PHP while it was easily available for Java, Python, Go, Node.js and all other popular backend technologies?
- Do you feel that you’ve reached a glass ceiling and there’s nothing more you can learn about PHP?
- Do you want to broaden your skillset to increase your market value as a software developer?
- Or maybe you just want to take a test drive in different technology to make a sensible decision whether it’s worth for you to stick to PHP or go for something else?
No matter the motivation behind you, it’s always a good idea to learn a different programming language, especially if you consider that it is incomparably easier to learn a second and every other one than to dive into the programming world as a newbie. In fact, PHP is frequently referred as a language with a steeper learning curve than Python so the transition should be smooth and relatively painless. At this moment you probably already have a solid base for jumping into the world of different technology, knowing all the good and bad practices of writing maintainable and testable codebase, being familiar to and making use of object-oriented design, following the SOLID principles. The good news is: these rules apply no matter the programming language you use.
We’ve already written on our blog about the Python vs PHP comparison, so it’s not the point of this article to duplicate these data. I’ll try to focus on the process of getting familiar with Python from the perspective of an experienced PHP software developer.
What’s similar about PHP and Python software development?
Dynamic typing
In PHP you don’t need to declare types of variables and as well you don’t need to worry about performing operations on variables that are not strictly the same type (with some limitations of course). Python also handles the types juggling for you, so from this point of view, the transition is quite seamless.
Type hinting and return types
Partial type hinting was available in PHP since the 5.1 version (allowed types: array, self, ClassName, callable
) but full support of type hinting and return types was introduced since the 7.1 version of PHP. This improvement in combination with a good IDE which is capable of highlighting incorrect data types is a powerful toolset when it comes to debugging.
// PHP >= 7.1
function do_something_with_value(int $value): int {
// some code ...
return $value;
}
That’s why I was very glad to hear that since version 3.5 Python (PEP 484) introduced a module called typing
which enables a software developer to declare types of variables and also define the return types of functions.
# Python >= 3.5
def do_something_with_value(value: int) -> int:
# some code ...
return value
Variable number of function arguments
In both: PHP and Python, you can assign a default value for a function’s argument and then call the function providing only the required arguments. Although thanks to Python’s **kwargs
it is much easier to override any argument’s value by name instead of overriding all positional arguments up to the one you actually need to override as it is in PHP.
Eloquent ORM & Django ORM (QuerySet)
When starting as a Python web developer, good chances are you are going to end up using Django as your first framework. Django is an open-sourced web framework which has a community of 12k+ people. Django uses a powerful ORM called QuerySet (or Django ORM). It follows an Active Record pattern. You might be familiar with this pattern if you had a chance to use Eloquent ORM used by Laravel. The idea behind both solutions is similar, but the way of using efficiently one and another is quite different. Although for those who were using Symfony only, this might be something new as it is quite a different approach compared to the Query Builder pattern used by Symfony’s Doctrine ORM.
High-level languages
Both: PHP and Python are high-level programming languages which mean that you don’t need to worry about memory management, garbage collection, and other low-level details which are being done for you automatically.
Differences
Code structure
In PHP you can indent every line of code with a different number of spaces/tabs (making your code almost impossible to read by the way) and everything is going to run perfectly as long as you have your semicolons and curly brackets in place. Python, on the other hand, emphasizes code readability. Code indentation is really important while developing software in Python so a good IDE with code structure highlighting might be very helpful here.
PHP (7.1 +)Python (3.5 +)Code blocks curly braces {}
whitespace indentation Variables$a = 3a = 3
Functionsfunction a() {def a():
Typingfunction a(int $b): bool { def a(b : int) -> bool:
Classesclass A extends B {class A(B):
Multiple inheritance
PHP doesn’t support multiple inheritance, but you can implement multiple-inheritance-like behavior in PHP by using Interfaces
or Traits
.
class ChildClass extends ParentClass {
use MotherTrait;
use FatherTrait;
}
Python, on the other hand, supports multiple inheritance out of the box.
class ChildClass(ParentClass, MotherClass, FatherClass):
pass
Switch statement
There’s no such a thing as switch
statement in Python! It might be a little surprising at first. The Python documentation proposes to use a dictionary mapping instead. As a result, this example code in PHP:
function sample_function_with_switch($a) {
switch ($a) {
case 1:
return 'You passed one';
case 2:
return 'You passed two';
default:
return 'You passed something else';
}
}
In Python would look like this:
def sample_function_with_switch(a):
options = {
1: 'You passed one',
2: 'You passed two',
}
return options.get(a, 'You passed something else')
Managing dependencies
In PHP-based web applications installing dependencies is being managed in most of the times by the Composer. By default, all dependencies are being kept in the directory named vendor
inside the project’s root. When having multiple projects on your machine simultaneously this way you can easily manage each project’s dependencies separately.
In Python world, there’s a package manager called pip. The main difference here is that pip
installs the packages for Python globally, not for your current project. So the quite obvious question appears: how to resolve conflicting dependencies when having multiple projects on your machine? The answer is: virtualenv. Virtualenv is a tool which creates an isolated Python environment with a separate Python binary and separate pip
. This way you can install Python dependencies only for a single virtualenv (each project separately) instead of installing them globally.
Versatility
Of course, there are more applications for PHP and Python than just writing yet another web service. PHP is claimed to be used for VR, AI, and ML but I personally haven’t met anyone who would pick PHP over Python for machine learning for example. Moreover, that Python, thanks to the huge number of available libraries to pick from, can be used for:
- scientific and numeric computing,
- education (thanks to its ease of use and clean syntax),
- desktop GUIs,
- software development,
- business applications.
So in terms of versatility, Python offers much more applications than PHP which should not be surprising as PHP was designed as a web development language while Python was designed as a general-purpose language
Should you become a Python developer?
Although there are probably much more aspects to be discussed when moving from one technology to another, I tried to highlight the ones that are worth to mention. When doing research for this article, I stumbled upon someone’s question about the software developers’ experience after moving from PHP to Python. People that answered it stated clearly that:
- they are way more productive after switching to Python;
- they have got a lot of fun with Python;
- if they were about to pick a language for starting a new project, they’d choose Python.
I hope this encouraged you to begin an adventure with a new programming language despite being a pro in PHP software development - I think it’s definitely worth a shot!
Navigate the changing IT landscape
Some highlighted content that we want to draw attention to to link to our other resources. It usually contains a link .