I'll take a wild guess here and assume that this question comes from the mind of someone trying to break into IT and, thus, aiming to maximize the payoff for their training efforts. It's totally understandable.
Do you want a more elaborate answer? I'm glad to provide it.
How to Measure the Quality of a Programming Language
To answer any question in the form of "Is X better than Y?", there must be an objective definition of better available. And that's when things get complicated, particularly when it comes to programming languages.
Before going into the details, I'd like to establish a few feature categories since the language features themselves are not enough to make a wise decision.
This is how I’d organize the items to be evaluated:
- Core features
- Purpose (Application that can be built using it)
- Basic syntax
- Running model
- Type system
- Memory management
- Data structures
- Object-Oriented Programming support
- Functional Programming support
- Running environment
- Dependencies (Web server, browser, etc.)
- Support available
Let’s dig a little deeper.
- The $ prefix for variables in PHP
- The -> instead of . for accessing object properties or methods
By running a model, I mean whether the language is supposed to be interpreted or compiled to run a program written in it. On this point, they are equal; they both depend on an interpreter to run. This is both a con and a pro.
A con because you need to have such an interpreter installed in your system if you are to run this kind of program.
A pro because this is how you, the high-level developer, achieve multi-platform support for free.
Which one is better is an open argument. I tend to lean towards safety over versatility so I’d say a strongly typed language is better—one point for PHP.
When it comes to memory management, both languages work in a pretty similar way. There's no explicit need to allocate memory, and a garbage collector takes care of releasing the unused objects.
PHP used to have very poor support for advanced data structures. It basically treated everything as an associative array. Since the engine rewrite (back in version 5), a more mature Object-Oriented model was introduced, making it a solid alternative.
More recently, a whole data structure library was brought into the language core, giving it really powerful tools.
Without going into many details, there's no clear winner here.
Object-Oriented Support Programming
In PHP, OOP is optional, and the distinction between primitive values and objects is strong.
This doesn't mean either one is better than the other, they're just different, and it's essential to understand this distinction to make the most out of each one.
Functional Programming Support
In the case of PHP, the choices are the official PHP binary or some web server-specific add-on (as in the case of Apache or PHP-FPM).
From the very beginning, PHP was meant to be used on the server-side to interact with data sources and produce HTML output.
Another important difference is the fact that PHP is not designed to serve HTTP requests on its own, meaning it will need a webserver to handle them and resort to it for the request processing. NodeJs is designed to handle HTTP requests by itself, which makes it somewhat easier to install.
One particularly interesting feature of NodeJs is its ability to treat I/O operations separately, thus achieving better performance in general as a program can continue in parallel.
Of course, this comes with a price in code complexity: parallel programming is not as straightforward as nonparallel.
A couple of projects aim to bring this kind of asynchronicity to PHP (like ReactPHP or Swoole).
One important point to make here is that, because of PHP’s running model, it’s very easy to deploy in shared hosting environments, making it a smart choice for professionals who don’t have access to their own hosting.
As mentioned above, both languages can be used to create Command-Line Applications.
When it comes to creating Desktop applications, neither PHP nor NodeJs has native support for it. Nevertheless, some projects aim to fix this, like ElectronJs and phpdesktop.
There are not many tools to build native mobile applications using PHP, although there’s no impediment for one to appear anytime.
In general, the PHP community is known for its friendliness with newcomers.
As you learned here, both languages are robust and mature tools that offer great features for developers, making them a non-trivial choice.
If you're just starting out, my advice is to just pick the one you feel more comfortable using and focus on learning the core concepts of programming. Should the time come to switch, those lessons will be with you for the long haul.
Trust me: even if they seem completely different at first, most programming languages have more similarities than differences. It's like playing the guitar or a piano. If you know your way around music, you'll eventually be able to switch without too much effort. Just as instruments are tools to produce sound, programming languages are tools to express algorithms.
Until next time, enjoy your coding!