IT Enthusiast — Is JavaScript better than PHP
source link: https://rodiongork.tumblr.com/post/109573287158/is-javascript-better-than-php
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Is JavaScript better than PHP
It is a quite popular thing to claim that PHP is a veeeeery baaad language. Often these claims are made by people who do not have much experience with PHP, but have read some great articles like PHP - a fractal of bad design.
When I ask “which language you recommend instead” - about 50% of these people shout JavaScript for sure! I always suspect there should be something wrong, since JavaScript has many features similar to PHP - c-like syntax, lack of strict typing and lack of complicated data structures offering instead array-on-steroids.
I myself extensively use both (as many as several others) and so I decided to make some comparison.
Can you guess why JavaScript sometimes looks similar to spork like this?
1. Basic values
PHP is popularly blamed for usage of two equality operators - double and triple ones. But it is just the same with JavaScript. Moreover you surely encountered many funny things like these:
- “ \t\r\n” == 0 // yields true
- “false” == false // yields false
- !!null == null // yields false
Of course JS have explanation for all such behavior. But it does not make more sense usually :)
2. Basic operations
This is really great. If you use some wrong operations on some wrong operands - in PHP you get an error so you can fix your mistake. But not in JS:
- sum of two arrays [] + [] yields “ (empty string)
- sum of array with object [] + {} yields an ojbject
- sum of object with array {} + [] instead gives you 0 (zero)
- and sum of two objects {} + {} is NaN (undetermined numeric value)
Subtraction behaves in a different manner, but you may have fun testing it yourself! Using empty string instead of array or object you may get few more curious results.
What is important - no error is generated! So if due to loose-typing your variables get some wrong values (it happens often enough, you know) - you will not at once detect something goes wrong.
3. Basic API
It is important to have a good library of tools at hand. Library functions provided by PHP are often blamed for performing too various functions and their names being spelled in different styles (due to inheritance from C).
Meanwhile in JavaScript:
- you have substring and substr and you never remember which is what
- if you want to format number (as with sprintf or number_format) you usually can only use few manual tricks like taking substring of number prepended with string of zeroes etc
- some methods behave differently across implementations - famous example is with split - less famous with forEach on arrays (not jQuery-based one) which in certain implementations (like Rhino) for array with only two indices 0 and 100000000 will enter a loop of billion iterations.
Of course all these are only minor annoying things, I just mention them to express my point of view that there is nothing JS could boast.
4. Object-Oriented Design
JavaScript simply has no proper implementation for OOP. Yes there are functions and their prototypes which could be used in some verbose and clumsy manner to mimic OO design in some or other way depending on your needs. But far not covering all fundamental principles of OOP.
Meanwhile PHP provides all things about classes, interfaces, methods, constructors, fields, packages, their visibility etc in very similar manner to how Java does this.
Of course JS-guys usually answer "we do not need it. we get used to prototype-based design” however it looks the same as grown individual who got used to diapers.
I’m sorry to say this, but when almost every contemporary language have all necessary features for writing OO-programs, it is strange to say that “we do not need this, we prefer mimic this with some makeshift substitutions”.
5. Using JS + Node + Mongo architecture
The popular thing is to say that with JS you can write the whole application in a single language. Just use it on client side, on server (as Node.js) and in the database (MongoDb).
However this idea looks nice only until you really try it. When you really try, you find out several nasty things:
- you still need to learn quite different versions of JavaScript - some front-end frameworks, NodeJs API and API for Mongo - so the same language behind them does not help much
- using NoSQL database for your business-data is ok only while you have few collections and need no complicated queries to them involving joins - usually you found this mistake too late because your customer asked a new kind of report and you see that hundreds lines of code should be written manually instead of three lines of SQL query; there is nothing to do with it - Mongo is a special database, for special purposes - and it is better to use it for such purposes, rather than try to fit it as a universal tool for every need.
- the last is most important for beginners - to host NodeJs server with MongoDb you have far fewer choices than with PHP and MySQL - for free you can try it mainly at Heroku and OpenShift - or you can use paid plans there, or purchase VPS, however with latter you’ll need to care of setup yourself.
So though it initially sounds interesting, really it appears that the only (vague) advantage this approach offers is “you use a single language”. But is it really an advantage? Look at this picture above - now you know why it is there!
It is one tool to be used for several purpose. But I bet you do not want it to be offered to you at restaurant. You prefer to use spoon for soup and fork for beef.
The same is with building web-applications - it is easier to learn several proper tools (e.g. HTML+JS at frontend, PHP at server and SQL for database) rather than to struggle with universal one.
Recommend
-
69
Adding tag support to Jekyll Now
-
31
Never Settle. Emblazoned on the website, promotional materials, and the home screenof the phone, this is what OnePlus and T-Mobile would like you to believe about thenew OnePlus 7t Pro 5g McLaren edition (as this is a long name, from here on out...
-
1
How I learned about Merklix trees (without having to become a cryptocurrency enthusiast) May 31st, 2019 In my distributed systems course a coup...
-
7
Vagrant + Ansible on windows: my experiments so far Recently i was asked to help someone run vagrant ansible combination on windows. Its a fun experiment coz ansible never claimed to support windows as control device and the solution...
-
25
Intel NUC 11 系列機種來了,NUC 11 Enthusiast 配備 NVIDIA GeForce RTX 2060搭載第 11 代 Core 行動處理器的 NUC,目前有 3 個延伸系列機種提供多樣性選擇。
-
4
Home iOS & Swift Tutorials Full-Time Indie iOS Dev and Music Enthusi...
-
7
Meet Simone Giertz: Inventor, robotics enthusiast, and YouTuber Even if you don’t follow Simone Giertz on social media or YouTube, there’s a good chance you know of her work. Originally hailed as the Queen of Shitty Rob...
-
5
NoRedInk – Haskell for the Elm EnthusiastMany years ago NRI adopted Elm as a frontend language. We started small with a disposable proof of concept, and as the engineering team increasingly was bought into Elm being a much better dev...
-
8
PHP Is Incredibly Better Than Its Other Alternatives For Web Projects Needless to say, PHP is one of the most popular programming languages in the world. With an incredible set of features such as web servers an...
-
8
@elizabethlvovaElizabeth LvovaSustainable Marketing Specialist at Evrone.com
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK