This post covers the Variables section of the PHP Basics chapter when studying for the Zend PHP 7 Certification.
In PHP, variables are represented by a dollar sign, $
, followed by the name of the variable.
$foo = 'bar';
They can contain letters, numbers and underscores, although must not start with a number.
By convention, variables start with lower case and then each word afterwards starts with a capital letter. Note that variables are case sensitive, so the following would print out different values:
$someVariable = "Something";
$SomeVariable = "Something else";
echo $someVariable; // Something
echo $SomeVariable; // Something else
One of the variables that cannot be assigned a value is $this
, which in Object Oriented Programming is a reference to the current object.
$this = 'that'; // Error
Variables by default are assigned by value, which means that the left operand gets set to the value of the expression on the right. It does not mean ‘equal to’ as the equals sign suggest.
Variables can also be assigned by reference, which means that both variables end up pointing at the same data, and nothing is copied anywhere.
References use the &
symbol.
<?php
$foo = 'Some value'; // Assign the value 'Some value' to $foo
$bar = &$foo; // Reference $foo via $bar.
$bar = "Some other value"; // Alter $bar...
echo $bar; // Prints out 'Some other value'
echo $foo; // Also prints out 'Some other value'
Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically. A variable variable
takes the value of a variable and treats that as the name of a variable.
<?php
$a = 'hello';
$$a = 'world';
The code above has defined two variables: $a
with contents hello
and $hello
with contents world
.
You don’t actually have to initialise variables in PHP however it is strongly recommended to do so. PHP will throw an E_NOTICE level error message if an uninitialised variable is used.
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
echo $somevar; // Throws a "Notice: Undefined variable: somevar" message.
The isset()
function determines if a variable is set and is not NULL. If a variable has been unset with unset()
, it will no longer be set.
isset()
will return FALSE if testing a variable that has been set to NULL.
<?php
$var = '';
if (isset($var)) {
echo "var is set"; // This prints out
} else {
echo "var is not set";
}
if (isset($otherVar)) {
echo "otherVar is set";
} else {
echo "otherVar is not set"; // This prints out
}
The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope.
For example, the variable $foo
defined below will be available in script.php
.
<?php
$foo = 'bar';
include 'script.php';
This is because the variable is contained within the same single scope. If however there are variables which are used inside a function, they are by default limited to the local function scope.
<?php
$a = 1; // Defined in the global scope
function someFunc() {
echo $a; // Reference to local scope variable
}
someFunc(); // No output
To resolve the issue, you can use the global
keyword within the local scope, which will mean that any references made to the variable will refer to the global version.
<?php
$var1 = 'Hello';
$var2 = 'World';
function greeting() {
global $var1, $var2, $var3;
$var3 = $var1 . ' ' . $var2;
}
greeting();
echo $var3; // Outputs: Hello World
PHP also provides predefined variables, some of which can be seen below.
$GLOBALS
— References all variables available in global scope.$_SERVER
— Server and execution environment information.$_GET
— HTTP GET variables.$_POST
— HTTP POST variables.$_FILES
— HTTP File Upload variables.$_REQUEST
— HTTP Request variables.$_SESSION
— Session variables.$_ENV
— Environment variables.$_COOKIE
— HTTP Cookies.$php_errormsg
– The previous error message.View the other sections:
Note: This article is based on PHP version 7.0.