Last updated on

My sister and I are very different.

She likes to be the life and soul of the party whilst i’m happy to sit back watch everything go by.

I like video games, she likes boxed sets.

She has long thick hair, I have… Well! Ahem! You know.

The point is we’re totally different.

Like chalk and cheese.

And the same is true when you compare PHP vs JavaScript.

PHP vs JavaScript

Some people when they are starting to learn web development want to know what languages they should be focusing on. A common question is “Should I learn PHP or JavaScript?”. Well luckily, this is a fairly easy one to answer. Because these languages are used in totally different places it’s easy to make a decision.


PHP is a server-side language. This means you write PHP code, upload it to your web hosting provider and when someone requests a PHP page on your site the code is run on the server. 9/10 this will result in a HTML page being generated by PHP printing out (or ‘echoing’) HTML tags which are then sent to a user’s browser.

Some common uses of PHP include:

The last point is quite an important one as a general user of your website will never see your PHP code. As the PHP code is stored and run on your hosting provider’s server the source of it is never displayed – just the result of any information that is printed out.


JavaScript is a client-side language. This means that JavaScript code that you write is loaded in to and run on the user’s browser. In contrast to PHP a general user of your site can access and see your JavaScript code. In general it will be used to perform an action or change a webpage’s content without reloading it.

Some common uses of JavaScript include:

It’s important to say that because JavaScript is working inside a user’s browser none of the changes it makes are permanent. Say if you had some JavaScript code that allows users to add more textfields to a form so they can provide additional information.

When you refresh the browser, the HTML page is downloaded from the server again and any changes made by JavaScript to the previous page are lost.

Hey, I heard you can use JavaScript on the server too!?

Yep, you’re totally right. It’s called Node.js. Most people when talking about JavaScript on the server will refer to it as Node.js to make the distinction between client-side and server-side JavaScript.

The reason why I have made the clear distinction in this article is purely because 90% of hosting providers don’t support Node.js server side code. However 99% of all hosting providers will support PHP on the server.

Similiarities and differences between PHP vs JavaScript code


Defining variables in JavaScript looks a bit like this (the var keyword isn’t necessary but it is good practice.):

var language = "JavaScript"; // A string
var number = 34; // A numeric value
var isThisJavaScript = true; // A boolean

In PHP this is pretty much the same although variables are identified with the $ sybmol:

  $language = "PHP"; // A string
  $number = 34; // A numeric value
  $isThisPHP = true; // A boolean

You can also see that both PHP and JavaScript use the two forward slashes // for comments


JavaScript implements arrays in a fairly standard way using the square bracket notation. You’ll notice they are heterogeneous as you can mix different types of values inside the array:

var array = [1,2,3,4, "JavaScript", true];

array[0] // Returns 1
array[4] // Returns "JavaScript"

PHP is also identical in the way you declare and access arrays. You can also use the array() function too:

  $array = [1,2,3,4, "PHP", true];
  $array = array(1,2,3,4, "PHP", true);

  $array[0] // Returns 1
  $array[4] // Returns "PHP"

For Loops

The for loops that PHP and JavaScript use are based on the C-style for loop but they have different ways of implementing a for-each loop.

for(var i=0; i<10; i++){
  // i increments each loop which holds the numbers 0-9

var array = [1,2,3,4, "JavaScript", true];

for(var i in array){
  // i holds the index value of the array not the actual value

With the JavaScript version the foreach loop doesn't give us each value of the array in turn, instead it just gives the index, the position of each element in the array.

  for($i=0; $i<10; $i++){
    // $i increments each loop to give the numbers 0-9

  $array = [1,2,3,4, "PHP", true]

  foreach($array as $item){
    // $item holds the actual value held in the array

The PHP foreach loop actually stores each array element in the $item variable each time.

While Loops

There's not a lot of difference between while loops with PHP or JavaScript.

var i = 0;

while(i < 10){

var i = 0;

} while(i < 10);

  $i = 0;

  while($i < 10){
    echo $i;

  $i = 0;

    echo $i;
  } while($i < 10);

As you can see from the above both PHP and JavaScript implement the while and do / while loops. Only the variable declaration and way the numbers are printed out is different.


Functions in PHP and JavaScript are identical.

function add(num1, num2){
  return num1 + num2;

  function add($num1, $num2){
    return $num1 + $num2;

  echo add(2,2);

The only change in the above examples is that PHP requires your variables to be prefixed with the dollar sign $. We could just copy the PHP function and paste it into JavaScript code but it's not usual to have dollar signs in JavaScript variable names. However this is permitted and would work!


There's quite a bit of a difference in the way that PHP implements objects compared to JavaScript. If you want a basic object in JavaScript you can do something like this with literal notation:

var user = {
  username: "james",
  password: "pass123",
  resetPassword: function(newPassword){
    password = newPassword;


However in PHP you first need to define a class from which an object can be created.

  class User{
    private $password;
    public $username;

    function __construct($username, $password){
      $this->password = $password;
      $this->username = $username;

    function resetPassword($password){
      $this->password = $password;

  $james = new User("james", "pass123");

  echo $james->username;
  echo $james->password; // Causes a fatal error as $password is private

This also allows for the incorporation of object-oriented principles such as encapsulation (using the access modifier private to restrict access to a variable inside the object).

Popularity and trends

PHP is at the moment in the top 10 of languages according to Tiobe index and is the 5th most popular language according to a survey of StackOverflow users last year.

JavaScript on the other hand is higher up in the Tiobe index. Also, according to the same StackOverflow survey more than half of developers surveyed said JavaScript was their favourite language.

According to Google Trends, the interest in PHP has been declining over time with interest in JavaScript staying fairly constant.

Career and salary differences

If you are looking to make a career as either a PHP or JavaScript developer you might want to consider the opportunities and salaries available.

According to the average salary for a PHP developer in the US is $86,453. They estimate that based on around 40,000 employees and job advertisments.

Compare that to the average salary for a JavaScript developer which is $109,905 according to However this figure is based on around 23,000 employees and job adverts.

The data might be a little skewed as I reckon a lot of those 'JavaScript developer' salaries are probably based on Node.js / server side roles. But, I think it's fair to conclude that there may be more PHP jobs out there but they are slightly less well paid.


So there you have it.

PHP is completely different to JavaScript in the way it is used to build websites and applications.

It might seem as though the future for PHP is on the downturn however with PHP being an integral part of hugely popular CMS platforms like WordPress, I don't think it's going anywhere soon.

JavaScript can complement a PHP driven site by providing client side interaction with web pages that are produced. There's no reason why the two technologies need to be against each other.

What are your thoughts after reading this? Are you going to learn PHP or JavaScript first? Let me know in the comments below!