Javascript for PHP Programmers: Variables

March 5, 2018

The easiest way for me to learn a new language is to compare it with PHP. And to write down what I learn.


$variable = "variable is global unless defined in a function";
$anotherVariable = "No need to specify type.";

function func()
    global $variable; // This is how you access global variable in a function.

    $localVariable = "This is local and not accessible in any other function.";


var globalVariable = 'value';
let variableWithLocalScope = 3;
const constantValue = 'you cannot assign new value to this';

PHP has var keyword too. It is used for assigning value when declaring class properties (variables). You can add public or private scope to PHP class properties.


class phpClass {
    public var $x;
    protected var $y;
    $private var $z;

© 2017 Amer Khalid