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
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
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:
let myString = "Adnan Babakan";
myString = myString.split(" ").join(", ");
// Result => Adnan, Babakan
than like this:
<?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:
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:
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
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
$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:
Top comments (28)
This idea that PHP is somehow unique in it's ability to allow you to write spaghetti code is absurd.
Have you not seen 10,000 line Javascript files of uncommented procedural code? I have. And oooo boy the hoops you have to jump through to write OO JS code in structured files is nuts.
Now granted, some languages provide more structure, or frameworks out-of-the-box... but whew JS is not one of those languages.
Writing bad PHP code is lazy programming or beginner mistakes. Let's stop shifting blame.
Couldn't agree more. Additionally, the more recent features and tools of PHP have made it a more pleasant experience.
Indeed, bad code is on the programmer not the language ( at least at the initial phase), there is a point as you learn more techniques, OOP, SOLID, PHP Unit, and good practices ,your code structure and refactoring gets better, you dont have to write spaguetti code unless you want to or are very new to programming,a programming language doesnt make you a good programmer from day 1. Some are easier to get started and thats where you see bad code spread everywhere, ex. Using deprecated mysql code instead of PDO or mysqli at least.
Thank you Juan for you comment. You are totally right and I couldn't agree with you more.
Thanks for you comment. And yes I agree with you too in some ways but still PHP's nature is the spaghetti code, though I'm a PHP programmer myself and more than half of my programs have been written in PHP XD
It's literally not.
Php mixed into html has been discouraged for like a decade now. Beyond that it's a procedural & OO language. Style or mess... is up to you.
Spot on. Rubbish code is on you as the programmer. If people are writing junk in PHP, they're writing it elsewhere too, especially JS.
Why on earth, are we in 2019 and still keep reading something between the lines of "PHP is Garbage"...
All the cons you mentioned are the result of bad coding practices and could be found no matter the language, except for the naming convention of its functions (I wonder what was going on) but that's it.
So let's stop this madness of PHP is bad because X Y Z...
I'm pretty sure that some years from now, people will say the same thing of Python, due to its actual popularity, a lot of new comers that will make mistakes and put some ugly code in the wild.
Sorry for the rant 😅.
Thanks for you comment Frank. Despite all the cons I mentioned about PHP I also mentioned some pros I love about it and that's one of the reasons I came back to PHP and this post was about my personal experience. Reading about comparison of two languages will still be there since programming world is growing. But to be clear again I'M A PHP PROGRAMMER TOO! XD
Nice post! But about the callback hell, I would not consider it a problem anymore, as
async
/await
, or even promises can make it go away.This is completely true in some cases but sometimes you would still need callbacks, it's somehow inevitable! XD
Actually no. You can use something like promisify or even build your own solution, but you don't need to use nested callbacks.
Thanks for sharing this with us. That's true but still it is a little bit of more work to be done anyways.
Nice article! In terms of php performance I'd like to mention something new maybe not many people know about:
reactphp.org/
I've did a test project and got to handle a big amount of traffic for a php script with a sql query.
Looking forward to test it in some real world projects.
Hey Alexandru! This library seems intense and I've not seen it before. Will try to test it on some personal projects and maybe write a post about it so everything will be fair enough. Thanks for sharing this with us.
I feel this article has not really needed. It's adding more ammunition to people that hate PHP for the sake of it. There is no point in really comparing the two technologies because as you've mentioned in your post - it depends on a number of factors such as infrastructure, project size, development skillset, budget etc.
Laravel is an excellent framework but it's not the only one available for PHP. The language has come on a long way since the early version days. It's time to embrace what PHP can do and not focus on what it cannot do.
Hi Alex, thanks for your comment. I see your concern about the filling up the gun for the people who hate PHP which I'm not one of them anyways XD.
Laravel isn't the only one absolutely and you are right. I've also worked with Phalcon which was absolutely pleasant.
None of your Node nonsense...Php can be clean and uncomplicated.
Php, Laravel, sass and vanilla javascript. Job done.
Minimal compiling, no jquery, no typescript. Secure login, easy eloquent db interactions, lovely migrating.
Hi. Thanks for your comment. This is only my personal experience and opinion. I am not forcing it on any one. XD
I just wanted to add a little comment about php. It's definitely more convenient in some ways, but then when you get to even slightly complex code, it becomes a huge burden -- especially the simple and common practice of putting a function inside a function.
Hey agamemnus! This is a point of view which I accept as well.
Thanks that was a good article, I like your sense of humor too 🤣👍
We've all seen spaghetti code in many languages. It can happen anywhere 😯
Hey Peter. Thanks for you comment. Sense of humor has made up my life by now XD. Spaghetti code is really common in every language and that is true but in PHP I see it more happen which is not PHP's fault anyways. LOL
Totally! My first internship when I got back into coding was with a company that was running a PHP based web app. And you want to talk about spaghetti 😫🤯
AND there were almost no comments, and no documentation!
It was the worst, plus they knew the tech stack was going to fail, but just kept throwing more spaghetti on top of it. Needless to say I left after not much time
Yeah that happens a lot. I had a somehow same experience in a company I was working for. Ask me about spaghetti. XD
I think I should change the post's name to spaghetti. Comments are all about spaghetti. LOL
Thanks Vitali.
You didn't mention your nationality on your profile page but based on your last name I suppose you are Russian if I'm not mistaken, anyways I hope you get to be the greatest programmer in your own country or maybe the world! XD
Very informative. However, splti not split? Typo?
Thank you very much Lauren. You read my code more precise than I did myself. Unfortunately Dev.to doesn't have an IDE for coding LOL.