Is JavaScript Better Than PHP? A Senior Engineer Compares the Two Technologies

by Mauro Chojrin

9 min read

Every time you go on social media, you'll find some form of this question. It may even be expressed in the opposite way: Is PHP better than JavaScript?

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.

If you're in this situation, I'm afraid I don't have an easy answer for you. The short answer is "No." Neither JavaScript is better than PHP nor PHP is better than JavaScript.

In fact, I’d make the same argument should the comparison be between any combination of JavaScript, PHP, Python, Ruby, or any other currently used language.

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.

So, let’s define a few features we can use to make the comparison somewhat sensible and easily decide when to use JavaScript or PHP.

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
    • Documentation
    • Community
    • Libraries

Let’s dig a little deeper.

Core Features of JavaScript vs. PHP

Purpose

Both languages are general purpose by design, meaning any application can be built using them. Both were indeed born in the web, and thus, some operations common to HTTP are built into them (Like the superglobals $_GET or $_POST for PHP or window.location.search for JavaScript). But still, desktop applications or command line scripts can be easily written using either.

Basic Syntax

When it comes to basic syntax, JavaScript developers and PHP developers should know that both languages share much of it (No surprise since they’re derivatives of C). The main differences are:

  • The $ prefix for variables in PHP
  • The -> instead of . for accessing object properties or methods

In general, I’d say JavaScript’s syntax is slightly less cluttered than PHP's syntax. One point for JS.

Running Model

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.

Type System

This is tricky as PHP has been moving into a stronger type system by enabling (but not requiring) the explicit definition of variable and function return types. At the same time, JavaScript remains a loosely typed language.

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.

Memory Management

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.

Data Structures

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.

On the JavaScript side, the language evolution followed a similar path and it wasn’t until ECMAScript Edition 6 that advanced data structures became available.

Without going into many details, there's no clear winner here.

Object-Oriented Support Programming

In JavaScript, objects were present from day one. In fact, everything is an object in JavaScript; in this sense, it's a more pure Object-Oriented language than PHP.

In PHP, OOP is optional, and the distinction between primitive values and objects is strong.

Another important difference is that PHP takes a more classic approach to OOP, meaning its model is based on classes, whereas JavaScript is prototype-based.

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

Both languages offer good support for Functional Programming. In JavaScript, this was the case from the very beginning. In PHP, it's more of a recent feature.

Running Environment of PHP vs. JavaScript

As mentioned before, both JavaScript and PHP are interpreted languages, meaning there’s the need for a program to run scripts written in either one.

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).

In the case of JavaScript, it depends on where the code is to be executed. If it’s for the FrontEnd, the web browser will be responsible for interpreting and running the code. On the other hand, if the code is supposed to be run on a web server or outside of a web environment altogether, there will be a need to use NodeJs.

Web

Comparing JavaScript vs PHP for web development, there are two sides to every application: FrontEnd and BackEnd.

JavaScript was born as a FrontEnd language, meant to be used for giving HTML pages more interactivity via visual effects.

From the very beginning, PHP was meant to be used on the server-side to interact with data sources and produce HTML output.

Some years ago, NodeJs saw the light and, with it, JavaScript made its entrance in BackEnd land.

This is, for many people, a deciding factor: JavaScript can be used both on the BackEnd and the FrontEnd, reducing the cognitive load for the developers. But this is true only to some degree since BackEnd development and FrontEnd development are still different in many ways.

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. 

CLI

As mentioned above, both languages can be used to create Command-Line Applications.

Desktop

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.

Mobile

There are not many tools to build native mobile applications using PHP, although there’s no impediment for one to appear anytime.

For JavaScript, there are a few, React Native, for instance.

Support Available for JavaScript vs. PHP

Documentation

Both PHP and JavaScript have extensive documentation available. The main sources are php.net and JavaScript MDN.

Community

Both languages are extremely popular nowadays. There are many online forums and user groups both for PHP and JavaScript.

In general, the PHP community is known for its friendliness with newcomers.

Libraries

JavaScript pioneered in the creation of a central location for code sharing and a dependency manager that made it really easy to bring third-party code into your projects (npm), but PHP features composer, which achieves the same goal.

Besides specific packages, both JavaScript and PHP have many mature Open Source frameworks built upon them, such as Symfony or Laravel for the case of PHP and Angular or Vue.Js for JavaScript.

Conclusion

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.

To expand your knowledge on both of the programming languages and their features, continue reading about how to implement form validation with JavaScript, how to build a headless Trello Clone with JavaScript, and how to use the Sublime text package.

Until next time, enjoy your coding!

FAQs

Q: Is PHP worth learning in 2021?
Definitely. PHP still powers over 80% of the current web, plus the new features available since version 7 made it an extremely mature tool.
Q: Is PHP faster than Javascript?
First of all, the language itself doesn’t define much about the speed of the applications developed using either, that has more to do with the underlying infrastructure. Another important point is that PHP is only available server-side while JavaScript can be used for both frontend and backend. If you are comparing BE JS to PHP, the big difference in favor of JS is its asynchronous nature, but that can also be achieved with PHP by leveraging ReactPHP or Swoole.
Q: Which is better Javascript or PHP?
Neither. They’re both good tools. The quality of a software application has little to do with the language it’s built upon but rather with the development practices applied along with its construction.
Mauro Chojrin
Mauro Chojrin
Senior PHP Engineer

Mauro is a PHP trainer and consultant. He has been involved in the IT Industry since 1997 in a wide array of positions, including technical support, development, team leadership, IT management, and teaching. He also maintains his blog and YouTube channel where he shares his knowledge with the world.

Expertise
  • PHP
  • Laravel
  • MySQL

Ready to start?

Get in touch or schedule a call.