How is it like to be back from Node.js to PHP?
Tags: php, node, javascript
Published: 2019-11-05 22:41:04

Hello there community!!!!
It's been a long time since I've written my last post because I was working on some projects full time and I didn't have enough time to write a post.

Anyways, recently I had a really pleasant experience of switching back from Node.js to PHP which was incredibly amusing in some ways; So I wanted to share it with you.

#PHP

![PHP](https://www.elegantthemes.com/blog/wp-content/uploads/2019/06/featured-php-7.png)

So as you know PHP is one of the weirdest programming languages out there in many ways, people love it and hate it, it powers lots of greatest softwares like WordPress for blogging and WHMCS for hosting management which are open-source and enterprise in respective order and also lots of poor code practices are also done by PHP programmers since it provides almost no structure. So to be said PHP is both a good programming languages and a bad one at the same time.

##Why I love PHP?
A good question! And the answer is because it is pretty easier to write a program using PHP than in any other programming languages and deployment is also almost no effort since almost 90% of hosting services providers support PHP and Apache web server.

Another reason can be that PHP has good ideas about code splitting which is absolutely nothing! It means if you include some PHP file in the other one you will be able to use it with no more actions taken. Not like JavaScript which has to be exported and imported.

Some PHP features are just awesome you might not find them easily in any other language! One of the things that I love the most about PHP is that it supports variable named variables! Imagine you have a loop of `$i` and you can implement it in a variable name and declare a new variable each time like `$my_var_$i` which is going to be `$my_variable_1`, `$my_variable_2` and so on! I know you can do this using arrays or similar things in other languages but still it is not the same feeling. LOL

##Why I hate PHP then?
As I said despite all the benefits PHP provides it comes with some costs as well! PHP is one of the most used programming languages and that is caused to be misunderstood with lots of people, especially beginner programmers, so the bad code practices go on and on and spreads around the community. PHP is a kind languages that doesn't care much about disciplines but it is being used against itself.

One of the most famous PHP haters' reason is the spaghetti code (which means a code without a proper structure in case you didn't know the meaning) and that is absolutely right to be honest! A PHP code causes into spaghetti code even for a senior developer in some cases too due to its nature. There are lots of tools to prevent this like Composer for dependency management or Laravel as a framework. But let's be honest you can make a wolf wear a sheep skin but it's gonna be a wolf on the inside.

Another thing I hate about PHP is that it literally has no convention for naming! For instance if you want to have a string's length you would use the function `strlen()` which is quite OK. So you think every function will be in the same naming convention but what about when you want to replace a string with another in a phrase or sentence or anything else? You have to use the function `str_replace()` and that's when you realize that you've been tricked!

#Node.js

![Node.js](https://upload.wikimedia.org/wikipedia/commons/thumb/d/d9/Node.js_logo.svg/1200px-Node.js_logo.svg.png)

So let's get into Node.js! My beloved programming environment.
As you know Node.js is the environment which runs JavaScript on V8 engine which is used for server-side stuff and so on. JavaScript is my most loved languages amongst all other languages since I'm a web developer and almost 80% of tools that are used for web developing are made on top of Node.js and JavaScript.

##Why I love Node.js/JavaScript?
JavaScript is an object-oriented programming languages and almost everything in JavaScript is an object which comes in handy in many ways. I rather split and join a string like this:
```javascript
let myString = "Adnan Babakan";
myString = myString.split(" ").join(", ");
// Result => Adnan, Babakan
```
than like this:
```php
<?php
$my_string = "Adnan Babakan";
$my_string = implode(", ", explode(" ", $my_string));
// Result => Adnan, Babakan
```
So both of them are the same thing in JavaScript and PHP respectively but the first one looks more clean in my opinion.
Another reason that I love JavaScript for is the asynchronous and synchronous behaviour it shows as you need. In JavaScript you can have event-based functions so they are run if needed and after a certain task is done.
The versatility of JavaScript itself as it is for front-end and back-end at the same time es maravilloso! (It is in Spanish don't judge me XD)

##Why I hate Node.js/JavaScript then?
So despite every and each good point JavaScript has it also comes with some issues as nothing in this world is perfect anyways.
One of the most dramatic problems about JavaScript is the callback hell! So this is exactly the same thing which makes event-based functions possible! You might be asking this guy is crazy! He mentioned it in the loving features part and yet mentioned it again in the negative points part? To be honest YES! Callbacks are perfect and really useful but since you want to make sure that you have access to some data you need to always call them in the callback if you have another function which uses the previous data you need to implement the function in the callback which itself might be a callback and so on! In this case your code is going too deep and mostly growing horizontally than vertically since you have to use tab and spacing characters consecutively! Look at the code below:
```javascript
fs.readdir(source, function (err, files) {
  if (err) {
    console.log('Error finding files: ' + err)
  } else {
    files.forEach(function (filename, fileIndex) {
      console.log(filename)
      gm(source + filename).size(function (err, values) {
        if (err) {
          console.log('Error identifying file size: ' + err)
        } else {
          console.log(filename + ' : ' + values)
          aspect = (values.width / values.height)
          widths.forEach(function (width, widthIndex) {
            height = Math.round(width / aspect)
            console.log('resizing ' + filename + 'to ' + height + 'x' + height)
            this.resize(width, height).write(dest + 'w' + width + '_' + filename, function(err) {
              if (err) console.log('Error writing file: ' + err)
            })
          }.bind(this))
        }
      })
    })
  }
})
```
Code is from http://callbackhell.com/ which is a website explaining more about callback hell.

So anyways this code looks damned and crazy but believe me it works perfectly fine! This code might look much better in PHP I believe!

Another problem about Node.js is the node_modules which is explained perfectly in the image below:

![node_modules meme](https://i.redd.it/tfugj4n3l6ez.png)

This it literally true! Even for a small landing-page you would get something about 100 megabytes of node_modules which aren't going to be needed in the production but still are heavy on your computer!

#Back to PHP from Node.js
![Back to PHP from Node.js](http://blog.andolasoft.com/wp-content/uploads/2015/05/impliment-node.js-by-Php.jpg)

So nevertheless it is time for my experience of coming back to PHP from Node.js to be told!

For some recent projects I've chosen PHP as my back-end due to some deployment limitations and so on.

So my primary IDE is PhpStorm which supports both PHP and JavaScript perfectly along side with HTML and CSS of course and the first thing I did was installing Laravel as my main framework. Believe me or not I've been so much into Node.js and JavaScript that I didn't even see Laravel's official website's new look until few days ago. PhpStorm recognized Laravel automatically and it was an awesome feeling.

The first thing I was trying to do was to include the libraries I installed using Composer and I was trying to do something like this:
```php
<?php
$exampleLibrary = require 'example-library';
```
So literally I was mixing up JavaScript with PHP and that was hilarious XD.
For a second I was like: _Adnan really? PHP was the first programming languages you learnt and this is how you treat it now?_ I was shocked how I forgot PHP syntax and workflow then I corrected my code immediately.

The second thing I was mistaken was that I was trying to export something to be able to use in another file!

This was a good experience I got from this project despite all the PHP's power. It is awesome to be back to PHP for some projects. But would I stay with PHP again? Maybe YES and maybe NO! This really depends on the project's scale and the project's need but I will not be biased about PHP or JavaScript anyways and would choose either of them if needed and my advice to you also is that choose what ever best fits you or the project. Never let the wrong side of internet guide you about this! Choose wisely about what you need and which of these programming languages can provide you the needed tools easier!

I hope you enjoyed this post and please let me know again if I'm wrong or if you have any other side of view on the comments section below!

BTW checkout my experience of switching from Nuxt.js to GatsbyJS here:
{% link https://dev.to/adnanbabakan/how-is-it-like-to-work-with-gatsbyjs-as-a-nuxt-js-developer-4pf %}