Why don’t people like PHP?
Matt Angelosanto
Posted on November 22, 2021
Written by Andrew Evans ✏️
PHP, a recursive play on Hypertext Preprocessor, is a general-purpose scripting language geared towards web development. Although it is considered dated by some, PHP is still used across the internet, for example, by large companies including Facebook, Wikipedia, Slack, and Etsy.
With the rise of JavaScript and the proliferation of frameworks and libraries like React, Angular, and Vue, many new developers are turning to those instead of PHP for several reasons. Inspired by this Twitter thread, we’ll discuss PHP and JavaScript in terms of features, capabilities, performance, and collaboration. Let’s get started!
PHP’s history
Originally developed by Rasmus Lerdorf in 1994, PHP has become more robust over time, following a regular release cycle stating in 2011 and introducing features like support for types, JSON, a command line utility, and just-in-time compilation. As a flexible server-side technology, PHP is applicable to a wide variety of use cases.
Given the state of JavaScript in the early 2000s, it’s clear why PHP was so widely adopted. As JavaScript matured as a language and other libraries and frameworks were introduced, developers began looking to JavaScript in lieu of PHP for their web development needs.
PHP offers many features that JavaScript includes right out of the box, like handling conditionals, looping, types, modularity, and objects. A major obstacle for PHP is that JavaScript tends to have a lower barrier to entry. For example, you can write and run JavaScript with just a browser and your favorite text editor. However, to use PHP, a scripted language, you’d have to use an interpreter and a runtime.
Implementing PHP
There are a few different ways to implement PHP, but one of the most popular ones is the Zend Engine. To use the Zend Engine, you’ll need a machine running a PHP compiler, then you can serve the Zend Engine from a hosted location.
With JavaScript, you can handle both your frontend and backend development with the Node.js runtime. Frontend projects run JavaScript right in the client’s browser, meaning that JavaScript code can be served from a CDN or a static file location, so you don’t need a full-fledged server to host your content.
Similarly, with npm, your backend projects also leave a very minimal footprint; you can either run them on a server or use serverless functions with different cloud providers.
JavaScript vs. PHP: Performance
JavaScript and PHP can perform similar functions, but under the hood, you’ll see how they really differ.
JavaScript is event driven with an event loop that allows for asynchronous activities. Instead of a synchronous run of JavaScript code where each line runs one at a time, JavaScript can run multiple sections of your code at once, preventing the main thread of your JavaScript programs from being blocked. Additionally, features like callback functions and the event loop allow you to handle multiple activities at the same time.
On the other hand, PHP is synchronous but allows for multi-threads. PHP requires each line of code to run in sequence, theoretically slowing down your programs. However, some may argue that PHP’s synchronous nature makes it easier to work with.
When you consider the benefits JavaScript's non-blocking implementation, I'd say JavaScript outperforms PHP in this area.
JavaScript vs. PHP: Integrations
JavaScript can be combined with many different web technologies, including HTML, XML, Ajax, and even JSX with React components. With npm and the Node.js runtime, JavaScript developers have a wide range of packages available at their fingertips. The leading frontend frameworks and libraries like Angular, React, and Vue each have their own features, which build even more on these integrations.
On the other hand, you can integrate PHP with HTML, which is popularized with CMS platforms like WordPress and Drupal. Additionally, PHP works well with the traditional LAMP stack. Integration with SQL is achieved through the MySQLi extension.
Generally, JavaScript and PHP can both run on any device with a web browser. Additionally, JavaScript has a backend capability with the Node.js runtime. There are several npm packages and frameworks that allow you to build JavaScript APIs that can run on the server side.
Both JavaScript and PHP can accommodate different needs, but in particular, JavaScript works well with a wide range of platforms without requiring too much extra work.
Setting up PHP requires a backend, as well as the necessary tags to wrap your statements if you want to use JSON data or injected code in your webpage.
JavaScript vs. PHP: Features
JavaScript and PHP offer many of the same built-in features, including:
- Conditionals
- Looping
- Conversion, i.e., string to integer, etc.
- Modularity, i.e., functions and/or methods
- Objects
The difference between PHP and JavaScript arises in the way that each implements its behaviors. With JavaScript’s non-blocking nature, your program can have low latency and handle many operations at once. Additionally, you can use things like promises or even RxJS observables to control behaviors.
JavaScript also includes the concept of scope, allowing you to control variables and data points. Scope is very powerful when you have a larger project with many modules or functions.
Let's try it with an example. The helloWorld
function has a locally scoped variable, message
, which is only available in the helloWorld
function. When you try to access the variable outside of the function, you’ll get an undefined value, allowing you to use the same variable name in multiple places as long as you respect the scope and use of that variable, which is a major advantage to JavaScript.
function helloWorld() {
const message = "hello this is a locally scoped variable";
console.log(message);
}
// this will output "hello this is a locally scoped variable"
helloWorld();
// this will output an error saying that the message variable is not defined
console.log(message);
With PHP, you typically use $
for variable names and follow similar constructs to JavaScript. PHP also lets you use things like echo
to write out values like the following snippet:
function helloMessage(string $messageValue): string
{
return 'hello '.$messageValue;
}
echo helloMessage('first message");
JavaScript vs. PHP: Collaboration
Both PHP and JavaScript have large communities with strong support. There are countless blogs and videos online that developers can turn to when getting started or solving problems. All of the major JavaScript frameworks and libraries, including Angular, React, and Vue, are open source and have development teams that regularly update their repositories. PHP is also open source, and many GitHub projects include PHP.
In terms of developer experience, JavaScript generally requires more background knowledge. To work with JavaScript, you typically have to understand things like the event loop as well as other web technologies like CSS and HTML.
These technologies are all part of the framework that runs the internet, therefore, learning them is not necessarily a bad investment. Once you’ve become familiar with JavaScript, you gain a wide range of options for your projects.
On the other hand, PHP is much simpler. Not only is it synchronous, but its models follow more traditional backend technologies, like Java and C. However, you still need to have a server or some environment running a PHP interpreter and usually the rest of the LAMP stack.
In terms of collaboration, I would say it's easier to work with a team on a JavaScript project, primarily because the tools that you use to do the job are minimal. With the advent of online coding platforms and environments, this is even easier. If you wanted to collaborate on a PHP project, you’d have to have a working environment set up first before you could run any code.
Which one is better?
JavaScript and PHP can both serve a wide array of needs. The fact that PHP is still so widely used shows that it has an endearing value to many websites. The argument shouldn’t necessarily be which one is better, but rather which one is better for your needs.
If your team is already working in the LAMP stack, PHP will probably be best for you. If your team doesn't know much about the LAMP stack and also has less backend experience, JavaScript is the way to go.
I personally enjoy using JavaScript for projects. I like how easily I can transfer skills from React to that of a backend Node.js API. It's also great to run code right in the browser, making it easy to get up and running with JavaScript across different platforms.
Many developers still like PHP and find great use cases for it. It really comes down to your individual team and project.
Wrapping up
In this post, we covered a high level discussion of PHP vs. JavaScript, reviewing the strengths and weaknesses of each. I hope you’ve enjoyed this article and have a better understanding of each. At the end of the day, the technology you choose depends on what works for you and your project. Both are great technologies and offer great value for many.
LogRocket: Debug JavaScript errors more easily by understanding the context
Debugging code is always a tedious task. But the more you understand your errors the easier it is to fix them.
LogRocket allows you to understand these errors in new and unique ways. Our frontend monitoring solution tracks user engagement with your JavaScript frontends to give you the ability to find out exactly what the user did that led to an error.
LogRocket records console logs, page load times, stacktraces, slow network requests/responses with headers + bodies, browser metadata, and custom logs. Understanding the impact of your JavaScript code will never be easier!
Posted on November 22, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.