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!
Jump ahead:
Originally developed by Rasmus Lerdorf in 1994, PHP has become more robust over time, following a regular release cycle starting 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.
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 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 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 and PHP offer many of the same inbuilt features, including:
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");
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.
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.
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.
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 see exactly what the user did that led to an error.
LogRocket records console logs, page load times, stack traces, slow network requests/responses with headers + bodies, browser metadata, and custom logs. Understanding the impact of your JavaScript code will never be easier!
Would you be interested in joining LogRocket's developer community?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowBuild scalable admin dashboards with Filament and Laravel using Form Builder, Notifications, and Actions for clean, interactive panels.
Break down the parts of a URL and explore APIs for working with them in JavaScript, parsing them, building query strings, checking their validity, etc.
In this guide, explore lazy loading and error loading as two techniques for fetching data in React apps.
Deno is a popular JavaScript runtime, and it recently launched version 2.0 with several new features, bug fixes, and improvements […]
27 Replies to "Why don’t people like PHP?"
Hello guys! Some things were not mentioned about PHP, for example the existence of libraries and extensions to work asynchronously, like ReactPHP and Swoole.
Did the author stop practicing PHP 10 years ago? That sounds so 2008 to a 2021 PHP developer!
To be fair, they also didn’t mention how JavaScript can be multi threaded. So it’s fairly even. Except web workers are part of the actual ES spec, unlike Swoole.
Agree with you John Doe.
You can’t compare JavaScript to PHP, they serve two totally different purposes. 2021 PHP is fantastic and it integrates nicely with React.
Why would they bother adding async support? Just bin it and move back to Perl.
What the heck? Is this a comparison?
In my experience, we don’t use Zend that much. PHP has some really solid frameworks like Symfony and Laravel. Also, PHP is an excellent option for making APIs using and ORM like Doctrine.
The comparison dies not match Javascript vs php that does not make sense. You can say nodejs vs php then you can compare. Ssr with frameworks can still be done with php if you want to but why would you since you can gain with both ends if you are laravel developer and decide to use live-wire. Swoole also gives php some features of nodejs event driven approach
Seems like the author used php once or twice a long time ago and never again, and didnt keep up to date on changes
Yep
You forgot to mention that JavaScript also supports multithreads as web workers. Best of both worlds.
I am very confused by this article. No mention that Javascript is front end and PHP is back end? And the title “Why Don’t People Like PHP?” But The article is a comparison of PHP and Javascript and not a discussion on what makes people not like PHP. How did you choose Zend as the most popular PHP framework? Laravel is arguably the most popular. How about mentioning that you can and probably should use both? I don’t know of many sites running on PHP that don’t have Javascript on the front end. It makes no sense that Javascript developers need to be familiar with HTML and CSS but PHP developers don’t. I understand the point of this article or what it is you’re trying to accomplish. A LOT more research should have been done.
It’s > 2015, javascript is just as strong back-end wise as PHP, though on the back-end i prefer GoLang!
Small point: PHP has very similar scoping rules to JS. You present this aspect of both languages as if PHP has none at all, this is very misleading. Furthermore, you neglected to mention PHP’s namespacing feature (which makes managing scope MUCH easier). This section of your article is deeply flawed.
I love using both languages, and I agree that the non-blocking io/async nature of JS is very powerful. But badly written JS is usually harder to fix than badly written PHP (callback hell etc).
FWIW, I’m the tech editor of PHP 8 Objects, Patterns, and Practice (Zandstra, 2020).
You could change the title of the article for: “why I like JS”
You all really seem to be trying a lot to press php put of the game. Till tomorrow, php is still outperforming JavaScript in web development. Talk about security, etc…
People don’t like PHP because its not the flashy new language. They make silly comparisons between PHP (a language) and a front-end framework (React, Vue, etc…). They make performance assertions based on NodeJS being asyncronous and PHP not. While PHP is run in the context of a webserver that is running multiple threads, each thread capable of executing your PHP code. The application model between nodeJS and PHP is completely different, you don’t necessarily need async or threading at the PHP language level.
Agree, a developer saying Javascript is fast because it is async has no clue on underlying engines. With opcache and optimizations starting with php7, php is one of the fastest scripting languages around on runtime.
This discussion is also cheesy as many veteran (and successful) coders/architects just choose the appropriate programming language according to the project and they generally have no problem on switching between different languages as they already know the main concepts. This is the discussion of junior coders on what to learn first which unfortunately author seemed to stay in that level.
There are many confusing things in this post that shows that the author does not fully understand PHP, or JavaScript for that matter.
First you can’t compare front-end JavaScript to PHP, and to that extent talking about fronted frameworks like React and Vue does not make sense either. You should compare backend features only.
There’s no interpreter and runtime, in PHP you only have the interpreter. It’s one thing. Also, there isn’t different ways to implement PHP, there’s only one. Zend Engine is a piece of the PHP core, it’s not an option and it’s not something that’s relevant in a comparison.
You could say that one of the advantages of JavaScript as a language is that it can be used in the front-end and back-end, but when comparing with PHP, only the backend makes sense.
Anyway, I hope people reading this article take time to read the comments because of the disinformation it brings.
The comment section is more informative than the post
Also like… Php has no scope?… One Google on W3 schools can tell you it does… And it works almost the same
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.
It looks like the author planned this post a long time ago. I don’t think the comparison between JS and PHP makes sense.
JavaScript only has 1 thread. It’s a event loop model. It cannot run multiple statement at the same time because it only has 1 thread. It’s concurrent but not **parallel**.
The post is very misleading.
You need to compare JS vs PHP from a real web apps perspective.
The main reason JS is so popular is that it can handle the frontend (there are many ways), backend (non-blocking), and heavy tasks (web workers). JS is skyrocketing after Node.js was released and web APIs were standardized (~2009, I knew about it in ~2012).
You can build an app in the JS ecosystem entirely.
You can even scale that app with no usage of other programming languages. When you find scaling solutions you are probably will be used other programming languages in infrastructure code.
The main reason for PHP decline – very long release cycle (6 years from v.5.3 to v.7 = 2009-2015) and lack of innovations. There are no new programming languages based on PHP that was released.
There was hope on FB (the Hack), but they give up too.
Now PHP can handle backend logic and do it pretty well on the lower scale (LAMP stack is alive). But there are new solutions that are more efficient on the big scale (Go, Rust, JS, etc).
I have started from C++ and PHP and now stick with JS/TS with minor adoption of Rust. C++ is also interesting, as a source of different conceptions and problem-solving solutions.
I think you can’t move JS from its top position 5+ years at least. JS is the backbone of the nowadays internet.
React is best framework because more people use it as compared to svelte BUT PHP is not better than JavaScript for same reason. Why?
Thank you all for saving my time