PHP For JavaScript Developers

PHP For JavaScript Developers

I started learning how to code in 2012 with HTML and CSS. Ever since the start of my coding journey, PHP has been one of the languages I’ve always wanted to learn. It’s everywhere. However, looking at it back in 2012, a freshly graduated high school student; I felt completely out of my depth. You can find out more about my journey here if you’re curious.

As a recent JavaScript-focused bootcamp grad, I’ve decided that I want to try to learn PHP again. It has been pretty difficult to find resources about PHP that aren’t focused on people who have never touched a line of code before. So, that’s why I’m writing this. I want to be able to help other people like me who just need a quick guide to the differences between their chosen language and the language they want to pick up.



General Syntax

One of the biggest differences between PHP’s syntax and JavaScript’s is that semicolons are required at the end of lines in PHP. I struggled with this a lot at first—still do sometimes—so that’s why I wanted to note it here first and foremost.

Using The Language

In JavaScript you don’t have to do anything particularly special to be able to run the code beyond making sure your file has a .js extension. However, in PHP you need to use tags, even in a file designated with the .php extension.

<?php 
# code here
?>

Declaring Variables

Creating a variable in PHP is super simple. Much like JavaScript, PHP is a dynamically typed language, and as such you don’t have to declare the type of the variable when you create it. It uses the $ symbol to denote variables.

$myvar = 'value';

By default in PHP any variable you declare is mutable. It can be changed absolutely anywhere.

Declaring Constants

PHP has a special function called define that is used to specifically create variables that can’t be changed. It takes two arguments: the name of the variable, and the value you want to assign to it. By default this function sets the variable name you create to be case sensitive. This can be overridden by passing true as a third argument to the function.

define('CONSTANT_NAME', value, true);

Declaring Arrays

Much like JavaScript arrays can be created with standard bracket notation or with a function in PHP. That said, PHP’s associative array is equivalent to a JavaScript Object, and is the only way to create a collection of key/value pairs in PHP without importing a module of some kind. Assigning a value to a key in PHP is denoted by =>.

$myArray = ['key1' => 'value', 'key2' => 'value', 'key3' => 'value'];

Functions
Functions in PHP are very similar to JavaScript (ES5 specifically).

function myFunc($param) {
    return $param;
}

The only real difference I’ve been able to find between the two languages in this regard is that PHP has an operator that changes the argument you pass in from being value based, to being referential: the &.

$myVar = 10;
echo $myVar; # displays 10

function addTen(&$param) {
    return $param += 10;
}

addTen($myVar);

echo $myVar; # displays 20

Loops

Much like Functions, loops aren’t that much different from the way they’re written in JavaScript. One exception being PHP’s foreach loop which changes based on the type of array you’re trying to loop over.

Normal Array:

foreach($arrayName as $item) {
    # do code
}

Associative array:

foreach($myArray as $key => $value) {
    # do code
}



Classes & OOP Methodology

Classes are one place where PHP differs from JavaScript quite heavily. Though PHP didn’t start out as an Object Oriented Programming Language—similarly to JavaScript—the functionality was added in later.

Access Modifier Keywords
In standard JS, modifier keywords aren’t needed for classes. For PHP, however, they are.

The Modifiers that you have in PHP are:

  • public – This can be used outside the class, either by a script or another class.
  • private – The class that created this is the only one that can access it.
  • protected – This can only be accessed outside of the class if it is being called in a class that is a child of the class this belongs to.
  • static – Allows the use of a property or method without the class that property or method is a part of having to be instantiated.

When creating a class in PHP it’s best practice to utilize these keywords to tell the class what it needs to do with attributes and methods on the class.

class MyClass {
    private $classAttrib;
    public function __construct($classAttrib) {
        this->classAttrib = $classAttrib;
    }
}

You’ll notice a few things in the above the code snippet. The first will probably be the two modifier keywords. Here, we’re declaring a private variable called classAttrib which will only be accessible via MyClass. The second, is the public keyword which we’re using in conjunction with PHP’s built in __construct method. This allows us to instantiate a class as though it were a function, just like we would in JavaScript.

$myClass = new MyClass(someValue);

This and The Arrow

Continuing with the MyClass example above, you’ll notice that we’re using this in the same way that we would in JavaScript. The difference here, is that we’re using an arrow (->) to access classAttrib on the class. We’ll also use this pointer to access anything on the class that we need to use throughout our code.

Here’s the same class in JavaScript:

class MyClass {
    constructor(classAttrib) {
        this.classAttrib = classAttrib;
    }
}

Getters and Setters
Getters and Setters are class methods used to get and set (or update) information to do with the class attributes. In JavaScript we don’t typically need to make them, and similarly they aren’t required in PHP. That said, you’ll see them far more frequently in PHP, so I thought it would be prudent to go over here. Basically, these methods are the only things that should be directly modifying or otherwise interacting with the class attributes outside of the class.

# ... inside MyClass
    public function setClassAttrib($classAttrib) {
        return $this->classAttrib = $classAttrib;
    }

    public function getClassAttrib() {
        return $this->classAttrib;  
    }

Inheritance
Inheriting from parent classes in PHP is similar to JavaScript, with the exception being that we don’t use super to pass in the parent class’ attributes. Instead we use the :: operator. Also called the Scope Resolution Operator.

class SecondClass extends MyClass {
    private $newAttrib;
    public function __construct($classAttrib, $newAttrib) {
        parent::__construct($classAttrib);
        this->newAttrib = $newAttrib;
    }
}



PHP & JavaScript Similarities

Now that we’ve talked about a fair few of the differences between JavaScript and PHP, let’s talk about some similarities!

  • PHP has spread syntax! You can use the exact same syntax as in JavaScript, in both arguments and arrays!
  • PHP has ternaries!
  • PHP has type coercion with the ==!

As I mentioned earlier, I’m still new to PHP, but I hope that this article was helpful to you! ❤️

Leave a Reply

Your email address will not be published.