Python to PHP: A Developer's Journey of Transition
Published: 2023-10-18 05:47:40

### Background
My journey as a developer took an unexpected turn when I joined a new organisation that primarily relied on PHP for its backend infrastructure. I found myself having to learn a new language. Having had a decent experience with Python and Django, this transition was quite an interesting experience.

Python and PHP, despite their differences, share a common trait - they are both interpreted languages. This means that instead of compiling the code into machine-level instructions, interpreters run through the program line by line, executing each command in real time. While the overarching concept remains the same, the journey from Python to PHP unveils some intriguing distinctions.

Let's delve into my experience as I navigated this transition, highlighting the contrasts and similarities between these two versatile languages.


### Installation
Navigating Local Development Environments
Three months ago, I embarked on a new professional journey, joining a vibrant team that had its unique approach to local development environments. In my previous experiences with Python, setting up my local development environment had been a breeze. However, transitioning to PHP was a different story altogether. 

For Windows users, the process of installing PHP was notably more complex than Python. I discovered that there were primarily two main methods to get PHP up and running. The team I joined predominantly used [XAMPP](https://www.apachefriends.org/) and [Laragon](https://laragon.org/docs/index.html), both of which presented their own set of advantages and challenges.

While Python installations were usually straightforward, it was an eye-opener to see that PHP often required more effort to configure on Windows. In contrast, Linux distributions like Ubuntu came with PHP pre-installed, simplifying the setup considerably.

In this blog, I'll dive into the intricacies of setting up PHP on Windows using XAMPP and Laragon, sharing my experiences and insights as I navigated this essential aspect of my transition from Python to PHP development.

On the Xampp Server, the URL looks like this: `http://localhost/project_name/v1/login`	
Whereas, in Laragon, It’ll be `project_name.test/v1/login`   [Looks good ;)]

### Software Design: MVT vs MVC
Laravel, a PHP web framework is based on MVC (Model-View-Controller) software design. In the case of Django, it’s MVT (Model-View-Template)
M - Model: Database (similar to M for Django)
This Model is the central component of this architecture and manages the data, logic as well as other constraints of the application.
V - View: UI, what the end-user will see. (similar to T for Django)
The View deals with how the data will be displayed to the user and provides various data representation components. 
C - Controller: Logic (similar to V for Django)
The Controller manipulates the Model and renders the view by acting as a bridge between both of them. 

### MVT Design
![MVT Design](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/30zr5mvaouhw82kynw6h.png)

### MVC Design
![MVC Design](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s24hcniq1q5qf80q08ch.png)

### Syntex 
**(One thing to remember! Syntex changes, Logic remains the same.)**
PHP and Python both have different syntax structures. 
E.g.
- Back to semi colons;
- Declaration of variables. 
    - Python: `varibable_name`
    - PHP: `$variable_name` (followed by $ symbol)
- `self` changed to `this`.
- Python has one of the strict rules of Indentation (4 black spaces). PHP doesn't follow this, It has a conventional way of using Curly Brackets to define a code block {}.

#### Python:

```
def sum(number1,number2):
    return number1 + number2
```
#### PHP:
```
function sum($number1, $number2)
{
    return $number1 + $number2;
}
```


The complete tutorial can be found here on the PHP tutorial website: [https://www.phptutorial.net/](https://www.phptutorial.net/)
Also, It is suggested to follow the Style Guide so that other developers can distinguish the names of variables, functions, classes, methods, and Exceptions etc. It provides rules and guidance for a given programming language, meant to ensure consistency from one project and one programmer to another. 
I was using [PEP8](https://peps.python.org/pep-0008/) for Style Guide for Python Code and now, I am following [PSR-12](https://www.php-fig.org/psr/psr-12/) for PHP code.

### Terminologies

| Python                       | PHP                  |
|------------------------------|----------------------|
| dist apps (common functions) | Services/Helper      |
| User/models.py | Models/UserModel.php |
| User/views.py | Http/Controller/UserController.php |
|User/renderers.py, serializer.py other misc|Services/UserService.php / Helpers/UserHelper.php|
|app_name/urls.py|routes/web.php|

### Basic Directory Structure

![BasicDirectoryStructure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i59b79itzpwe4n2uydm0.png)


### Dependencies: Xampp or Laragon
While setting up a project in Xampp, don't forget to make changes in php.ini to install services like MongoDB or 3rd party services. ([PECL:: The PHP Extension Community Library](https://pecl.php.net/) one-stop for Extension Library)
Python supports package managers like pip or conda. pip is widely used PM. PHP uses composer. Composer is a tool for dependency management in PHP.
For example:
Python: `pip install pymongo`
PHP: `composer require mongodb/mongodb`

### Database
Django used SQLite DB by default and PHP used MySQL.
Here, syntax remains the same. Both frameworks work well with SQL Databases. 

### The problems I faced in the beginning:
1. Structure of MVC
2. To understand, inbuilt methods inside Laravel
3. Debugging doesn't work well for PHP7
4. I had to re-install Xampp several times due to the unavailability of the port. Xampp uses port 80
5. Issued problem while changing Virtual Host in _httpd-vhosts.conf_

_Pt 4 & 5 were solved in Laragon._

---

Furthermore, there is another crucial element that significantly impacts success, and that is the unwavering support provided by the team. I am grateful to report that I have been fortunate to receive exceptional support from my senior leadership team. 

For those just starting in a new environment, beyond adapting to the latest technology, a key source of motivation lies in the practice of code review. Code review not only cultivates a collaborative atmosphere and enhances learning, but it also fuels motivation by fostering a strong sense of responsibility and a commitment to producing top-notch, well-structured code. This process encourages a culture of ongoing improvement and excellence within the team.

I can quote a passage from '[The Software Engineer’s Guidebook](https://www.engguidebook.com/)' by [Gergely Orosz](https://twitter.com/gergelyorosz) that discusses the importance of code reviews for new team members

![New Joiners and Code reviews](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1hrwyvs75loedcl7tsky.png)
 
---
As I continue my journey as a PHP developer, I look back at my first three months with gratitude for the opportunities that come with change. Along with Python, PHP has been a transformative experience, pushing me to embrace new challenges and expand my horizons in the world of programming. I eagerly anticipate the adventures and growth that the future holds in this new role.

---
Links which I used and found useful.
- [text] PHP Blogs: https://dev.to/t/php
- [text] Easy to read Best Practices: https://phptherightway.com/
- [text] W3 Schools tutorial: https://www.w3schools.com/php/
- [video] Tutorial by freeCodeCamp [English]: https://youtu.be/OK_JCtrrv-c
- [video] Tutorial by WsCubeTech [Hindi]: https://youtu.be/xIApzP4mWyA

_Me now onwards:_ :D
![meme_to_use_php](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ymuxbax6scyisz05n1m9.jpeg)


Feel Free to connect with me on Twitter/X -> [@jethwa_smit](https://twitter.com/jethwa_smit)

_Images are from respective owners. Framed with the help of ChatGPT_