PHP variables can be declared anywhere in the script. The part of the script where the variable can be used is what we called Scope.

Three different variable scopes:

local
global
static

Local Variable

A variable that is declared inside a function has a Local Scope and can be used only within that function.

Example


<?php
function my_function(){
	$var = 5;
	//$var can be accessed only inside this function
	echo $var;
}
?>

Global Variable

A variable that is declared outside the function has a Global Scope and can be used outside the function.

Example


<?php
$var = 5; //global scope

function my_function(){
	//$var cannot be used here
echo $var; //print nothing
}

echo $var; //print 5
?>

The global keyword

A global keyword is used to access a global variable inside a function.

You must first initialize the variable inside the function by adding the global keyword before the variable.

You can define multiple variables as global at once by separating them with comma.

Syntax: global $variable1, $variable2, $variable3;

Example


<?php
$var = 5; //global scope

function my_function(){
	global $var;

	echo $var; //print 5
}
?>

Static Variable

Normally after the function is executed, all of its variables are deleted. Somehow, we want some local variables to be available for further usage.

You need to use the static keyword to do this.

Add the static keyword before the variable when you first declare local variable.

Example


<?php
function my_function(){
	static $a=9;
	echo $a . '<br />';
	$a++; //increment 1 to the last value after function is executed
}

my_function(); //prints 9
my_function(); //prints 10
?>

How to take Cialis it seems to me that would already be time to cease to discuss this question. As I already answered it not once. Have it in a look. You shouldn’t worry! All deer go south when they are called by homesickness. Are turned and lie on snow till some hours.