PHP Variables, Constants & Operators

PHP Variable

PHP Variable Assignment by Value or by Reference

By default, PHP assignment is by value

$a = $b;
  • Changing the value of $b later will have no impact on $a

To assign a variable by reference without creating a duplicate copy:

$v1 = 10;
$v2 = 20;
$v3 = &$v1;                           # $v3 is alias to $v1 (Assign by reference)
$v3 = 30;                             # Both $v1 and $v3 are changed

echo $v1, ' ', $v2, ' ', $v3;         # 30 20 30

unset($v3);                           # Break up the reference link & unset $v3. However, $v1 remained as 30

echo $v1;                             # 30

Multiple Assignment

$a = $b = 5;

PHP Variable Scope (Global vs Local)

$v1 = 2;                              # Global scope
$v2 = 3;
$result = 0;

$v4 = 5;

function product()
{
    global $v1, $v2, $result;         # Access the global scoped variable

    $result = $v1 * $v2;              # 2x3

    $v4 = 7;                          # Since $v4 is not defined in 'global',
                                      # a new local variable is created
}

function product2()
{
    $GLOBALS['result'] = 2 * $GLOBALS['v1'] * $GLOBALS['v2'];   # Alternative way to access the global variable
}

product();
echo $result;                         # 6
echo $v4;                             # 5

product2();
echo $result;                         # 12

PHP Pre-defined Variables

function get_id()
{
    global $HTTP_POST_VARS;           # Access PHP predefined variable

    echo $HTTP_POST_VARS['uid'];

    echo $_POST['uid'];               # Some PHP predefined variables are visible within a function
                                      # even without "global" declaration
}
PHP Built-in variables Description
$GLOBALS Variables in global scope
$_SERVER Server and environment information
$_GET HTTP GET
$_POST HTTP POST
$_FILES HTTP File Upload
$_REQUEST HTTP Request
$_SESSION Session
$_ENV Environment
$_COOKIE HTTP Cookies
$php_errormsg Previous error message
$HTTP_RAW_POST_DATA Raw POST data
$http_response_header HTTP response headers
$argc The number of arguments
$argv Array of arguments

PHP Static Variable

function increment()
{
    static $v = 0;         # Static variable: Initialize only once to 0 when increment() is first called
                           # Static variable CANNOT be initialized by an expression like 2*10
    echo $v;
    $v++;
}

increment();               # Output 0
increment();               # Output 1

PHP Variable of a variable

$v = 'hello';
$$v = 'hi';                # $v evaluate to hello and then use as the name of a new variable
                           # $hello is set to 'hi'

var_dump($hello);          # string(2) "hi"

PHP Constant

define('MY_CONSTANT', 'hello ');    # Define a constant
echo MY_CONSTANT;                   # 'hello '

const A_CONSTANT = 'world';         # Use const to define a constant (PHP 5.3 or above)
echo A_CONSTANT;
PHP Predefined constant Description
__LINE__ Current line number
__FILE__ Current file name
__DIR__ Current directory
__FUNCTION__ Current function name
__CLASS__ Current class name
__METHOD__ Current method name
__NAMESPACE__ Current namespace

PHP Operators

PHP Operators Description
clone new Object creation
++ -- Increment/decrement: Works for integer and string - "ab"++ is "ac"
~ -  
@ Error suppression
instanceof $a instanceof SomeClass
!  
<< >> bitwise shift
<> same as !=
== != Equal and not equal (content comparison)
=== !== Equal/(not equal) if they have the same value and type
& ^ | bitwise operation
&& || logical operation
? : $condition ? $true_value : $false_value
and xor or  

Tradition operator like + - / * <= are skipped here for simplicity

Error Control Operator

"@" surprise an error

$file = @file('notexsit.txt') or die ("Failed: '$php_errormsg'");

Execute a OS command in PHP

$directories = `ls al`;
var_dump($directories);