PHP Variables

PHP Variables are represented by a dollar sign($). Variables store the information, it's like container means store something

  1. PHP variable starts with $ sign
  2. PHP variable name starts with letter or underscore, A variable can not start with number.
  3. PHP variable name can any number of letters, numbers, or underscores(A-z,0-9 and _)
  4. A variable name can not contain spaces between name.
  5. PHP automatically assign data type to the variable, it is depend on value. PHP is a Loosely Typed Language.
  6. PHP variable name is case-sensitive, $name and $NAME both are different

PHP variable declaration

As per below example, $website is variable name and it is hold "qoify.com"

 $website = "qoify.com";

PHP Predefined Variables list

$GLOBALS$_SERVER$_GET$_POST$_FILES
$_REQUEST$_SESSION$_COOKIE$_ENV$php_errormsg
$HTTP_RAW_POST_DATA$http_response_header$argc$argv

PHP Variables Scope

  1. local
  2. global
  3. static

local scope

When a variable is declared within funaction, then variable scope is local.

function local_scope()
{ 
  $a=5 ; /* local scope variable */ 
} 
local_scope();
echo $a;
//Output :: Notice: Undefined variable: a

The above example will not any value of $a because $a has local scope in function

In local scope, we can declare same variables name in different functions.

Variables scope also expand in included/required files as well

include 'test.php';
echo $a;
//Output :: 5

I above example, we have created one file "test.php" and declare a variable "$a", and it's include in another file. $a variable will be available


global scope (PHP global keyword)

When the variable is declared outside of a function and access global variable inside function, use the global keyword.

Example 1- global keyword

$a=4;
function global_scope_test()
{
	global $a;// global access
	$a = $a+5;
}
global_scope_test();
echo $a;
//Output :: 9

Example 2- With the use of $GLOBALS

$a=4;
function global_scope_test()
{
	$GLOBALS['a'] = $GLOBALS['a'] + 5;
}
global_scope_test();
echo $a;
//Output :: 9

Example 3- superglobals variable and scope

$GLOBALS$_SERVER$_GET$_POST$_FILES
$_REQUEST$_SESSION$_COOKIE$_ENV
$a=4;
function superglobals_test()
{
echo $_SESSION['name']; // This is superglobals variable, we can get the value of name variable
echo $_POST['age']; // This is superglobals variable, we can get the value of age variable
}

static scope (PHP static keyword)

PHP static variable used for the local variable not to be deleted after program execution is done, A static variable exist in local variable scope, use the static keyword.

Example 4- PHP static variable

//without static keyword
function test()
{
	$a=5;
	echo $a;
	$a++;
}

test(); // 5
test(); //5

//with static keyword
function static_test()
{
	static $a=5;
	echo $a;
	$a++;
}

static_test(); //5
static_test(); //6

Example 5- PHP declaring static variables

function static_test()
{
	static $a=5; // correct
	static $b = 5*4; // correct
	static $name = trim(' ABC'); // Incorrect beacause trim is function
	echo $a;
	$a++;
}

Variable's variable ($message and $$message in php)

$message = 'Hello';
$$message = 'World';
echo $Hello; // World
echo "$message $Hello"; // "Hello World"
echo "$message ${$message}"; // "Hello World"

Reference : PHP