Variables

Variable Scope

PHP and JavaScript take two very different approaches to declaring variables. In PHP, all variables are local in scope unless declared as global. JavaScript is opposite, and all variables are global unless declared with the var keyword.

Variable Types

Both PHP and JavaScript are loosely typed, meaning a variable can be of any type, and change from one type to another. However both PHP and JavaScript keep track of the type of variables, and you can check this type.

Casting Variables

Every now and then you might need to cast variables to a specific type. This is extremely important when dealing with JavaScript's + operator, which is used for both string concatenation and for numeric addition.

PHP
In PHP, variables may be cast to certain type by using parenthesis. String concatenation is done with "." and addition with "+".

JavaScript
JavaScript has functions specifically for casting variables to numbers. Both string concatenation and addition is done with "+". If mixing a string and a number with "+", concatenation will take precedence over addition.

Checking for NULL or empty() values

Variables in PHP don't have to be defined for you to use them, though if you're working with E_ALL compliance on (not the default of most PHP installs), your script will throw a notice if you try to use an undeclared variable. JavaScript is a bit mixed concerning undeclared variables, if you attempt to modify or compare with an undeclared variable, the script will break entirely, but you can check the variable status using typeof() or in conditional statements containing only that variable.

PHP

// Check if a variable is declared at all.if (!isset($foo)) {
$foo = TRUE;
}
// Or check if a variable has a value that equates to FALSE.// This includes variables that have not been declared.if (empty($bar)) {
$bar = TRUE;
}

JavaScript

// Check if a variable is declared at all.
if (typeof(foo) == 'undefined') {
var foo = true;
}
// Or check if a variable has a value that equates to false.
// This includes variables that have not been declared.
if (!bar) {
var bar = true;
}
// However an undeclared variable can't be used in comparisons.
if (baz == false) { // Variable undefined error.
var baz = true;
}

Boolean Variables

A simple but important thing to remember is that JavaScript only recognizes the keyword true in all lowercase. PHP accepts both uppercase and lowercase.

Objects and Arrays

In PHP, objects and arrays are two distinctly different things and have different syntaxes. In JavaScript, objects and arrays are often interchangeable, and you can switch between syntaxes freely.

Declaring an Object or Array

There are a few different ways to declare an object or an array in both JavaScript and PHP. The key difference between PHP and JavaScript is that JavaScript does not have associative arrays. Arrays in JavaScript are always numeric based. However, since objects may use array-like syntax, simply declare a new object when you'd use an associative array in PHP.

As you might notice in the last example, declaring an object in JavaScript uses the format commonly known as JSON, which stands for "JavaScript Object Notation". JSON strings having become very popular as a faster alternative to XML, and can be read and created with the PHP functions json_encode() and json_decode().

Object and Array Syntax

JavaScript and PHP are very similar in array notation, though they differ more in their object notation. The key difference is that PHP uses an array "->" to reference items within objects, while JavaScript uses the dot ".".