In this tutorial we are going to discuss about variable. We can distribute the variable in the following
categories:-
Basics
Predefined Variables
Variable scope
Variable variables
Basic:-
In PHP we define the variable with the ‘$’ sign. The variable name is case sensitive. A variable name
always starts with the letter and underscore.
Example
$a = 'Mike';
$a = 'John';
Predefined Variables:-
PHP provide the predefined variable. Mostly these variables are globally accessible.
Example:-
Superglobals — Superglobals are built-in variables that are always available in all scopes
$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
Variable scope:-
Variable scope means the context in which variable accessible. We are trying to explain the scope with
some example:-
Example:-
$a = 1;
include 'b.inc';
In the above example ‘$a’ is global or accessible for include file.
$a = 1; /* global scope */
function test()
{
$a=5;
echo $a; /* reference to local scope variable */
}
test();
echo $a;
Above
the above variable ($a = 1) scope outside the function. It’s not effective
from function variable.
If you want to use the global variable with in function then you need to use
the
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
The above result is ‘3’.
Variable variables:-
Some time we need variable variables for dynamic uses. First we take a normal variable. After assign
the value we again assign the value but this time we use ‘$$’ with the variable.
Example:-
$a = 'hello';
$$a = 'world';
echo
categories:-
Basics
Predefined Variables
Variable scope
Variable variables
Basic:-
In PHP we define the variable with the ‘$’ sign. The variable name is case sensitive. A variable name
always starts with the letter and underscore.
Example
$a = 'Mike';
$a = 'John';
Predefined Variables:-
PHP provide the predefined variable. Mostly these variables are globally accessible.
Example:-
Superglobals — Superglobals are built-in variables that are always available in all scopes
$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
Variable scope:-
Variable scope means the context in which variable accessible. We are trying to explain the scope with
some example:-
Example:-
$a = 1;
include 'b.inc';
In the above example ‘$a’ is global or accessible for include file.
$a = 1; /* global scope */
function test()
{
$a=5;
echo $a; /* reference to local scope variable */
}
test();
echo $a;
Above
the above variable ($a = 1) scope outside the function. It’s not effective
from function variable.
If you want to use the global variable with in function then you need to use
the
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
The above result is ‘3’.
Variable variables:-
Some time we need variable variables for dynamic uses. First we take a normal variable. After assign
the value we again assign the value but this time we use ‘$$’ with the variable.
Example:-
$a = 'hello';
$$a = 'world';
echo