Variables
- Variable is Special type of container which can hold the value .
- A variable consists of a name that you can choose, preceded by a dollar ($) sign.
- The variable name can include letters, numbers, and the underscore character (_).
- Variable names cannot include spaces or characters that are not alphanumeric, and they should begin with a letter or an underscore.
The following code defines some legal variables:-
$var;
$_635263;
$abbABC;
$num = 8;
print $num;
PHP Variable Scopes :
Local Scope :
A variable which is declared within a PHP
function is local and can only be accessed within that function.
Example :
<?php
function myFun()
{
echo $var; // local scope
}
myFun();
?>
Global Scope :
A variable which is defined outside
of any function, has a global scope.
Example :
Example :
<?php
$var=10; //global scope
function myFun()
{
echo $var; // local scope
}
myFun();
?>
Static Scope :
Static variable never lose its value whenever
function exists. Saving State Between Function Calls with static
variable.
Example :
<?php
function myFun()
{
static $x=0;
echo $x;
$x++;
}
myFun();
myFun();
myFun();
myFun();
?>
Parameter Scope :-
A parameter is a local variable
whose value is passed to the function .
Example :
<?php
function myFun($y)
{
echo $y;
}
myFun(5);
?>



