PHP and JavaScript: which tasks does each handle?
The two languages are often pitted against each other, as if you had to choose. In a web application they work in different places and have different tasks. Anyone who knows the boundary spots many errors before they are built.
What you will learn
Two languages, two places
JavaScript runs in the visitor's browser. It can display, show and hide, react to input and load content. PHP runs on the server. It can read and write data, enforce permissions, query other systems and make binding decisions.
In the browser · JavaScript
- Point out implausible input immediately
- Show and hide fields
- Load content without rebuilding the page
- Presentation and operation
On the server · PHP
- Validate input bindingly
- Enforce permissions and responsibilities
- Store, read and change data
- Emails, interfaces, background work
The assignment follows a simple question: must the result be reliable, even if someone modifies the program on their own device? Then it belongs on the server. If it is about ease of use, the browser is the right place.
An example from practice
A discount field in the checkout
A voucher field is supposed to show the price immediately. So JavaScript calculates the discount in the browser and shows the new total — pleasant and fast. But this figure must not be binding: anyone who modifies the page would otherwise get the price they set for themselves.
Both is right: the browser shows a preview, the server recalculates the order. If the results differ, the server prevails and the interface explains the difference. The same rule applies to shipping costs, volume discounts and minimum order values.
Three errors from mixing them up
But JavaScript also runs on the server, doesn't it?
Yes. With Node.js, JavaScript can be executed server-side, and many applications are built that way. This shifts the choice of language, but not the boundary: what remains decisive is whether code runs on a device the visitor controls or on one you control.
For the question "which technology should our project use?", the language is therefore rarely the first topic. More important are the systems involved, the existing interfaces, operations and the question of who will maintain the application in five years.
Common misconceptions
Further sources
- PHP manual (German-language) official documentation of the PHP project
- MDN Web Docs: JavaScript reference for the browser side
About the author
Fabian Lücker is managing partner of PHP-Schmiede KG in Rheine and has worked in software development since 2007. He is responsible for development and concept design and writes here about decisions that come up in real projects.
Unsure which technology suits your project?
We assess the question on the basis of your processes and the systems involved — not on the basis of a preference.