PHP Variable

In PHP, a variable is represented by a dollar sign followed by the name.

  • A variable name is case sensitive.
  • A variable does not have a type declaration. Its type is determined by its value.

Assign

Assign by value

This is the general assignment, assign a value to a variable.

$name = 'John';
$foo = 12;

Assign by reference

In this way, a new variable just references (meaning “alias for”) an original variable. Changes to the new variable affect the original, vice versa.

$a = 'Apple';
$b = &$a;
echo $b; // Apple

# Change $b
$b = 'Banada';
echo $a; // Banada
echo $b; // Banada

# Change $a
$a = 'Cherry';
echo $a; // Cherry
echo $b; // Cherry

The variable referencing other variable can not be assigned by reference. Only named variables can be assigned by reference.

Default value

If a variable is not initialized, it have a default value of its type depending on the context in which it is used.

var_dump( $unset_var );
// output:
// PHP Notice:  Undefined variable: unset_var in php shell code on line 1
var_dump(  $unset_var );

$unset_str .= 'abc';
var_dump( $unset_str ); // output: string(3) "abc"

Note: It is a security risk to rely on the default value, especially it is included into another file which has the same variable name.

Good practice

Always initialize a variable, give it a default value.

Variable variable

A variable variable is the name can be set and used dynamically.

For example, use the value $a as the name of another variable. The latter one is a variable variable.

$a = 'month';
$$a = 'January';

echo $$a; // January
echo $month; // January

// Used in an array
// $$a[1] will be ambiguous:
// the variable variable name is $a[1] or $a and it is an array.
// Use {} to resolve the ambiguity.
${$a[1]}; // the variable variable name is $a[1]
${$a}[1]; // the variable variable name is $a

Variable variables can not be used with PHP’s Superglobals arrays (built-in global variables ) within functions or class methods.

Scope

A variable’s scope is the context within which it is defined. The scope includes included and required files.

However, different from other languages, to access variables from global scope within a function, you need declare them global. Because variables used within a function is by default limited to the function scope. For variables inside a block, they are still available outside the block.

$a = 1; // global scope

include 'inc.php'; // $a is available in inc.php

function test() {
    echo $a; // $a is not available
    global $a;
    echo $a; // $a is availabe now
}

for( $i = 0; $i < 10; $i++ ) {
    if ( $i === 0 ) {
        $started = true;
    }
}

// 10
echo $i; // $i is still available outside the block
// true
echo $started; // $started is still available outside the block

Static variable scope

A static variable exists only in a local function scope, but it does not lose its value when leaving this scope.

function my_count() {
    static $n = 0;
    $n++;
    echo $n;
}

my_count(); // 1
my_count(); // 2

Note:

If a static variable is assigned by reference, it’s not remembered.

Predefined Variables

There are a large number of predefined variables to all scripts.

Superglobals

Several predefined variables are superglobals which are available in all scope. There is no need to declare them global within functions.