Types of Variables in php
PHP Variables
v
In PHP, a
variable is declared using a $
sign followed by the variable name.
Ø As PHP is a loosely typed language,
so we do not need to declare the data types of the variables.
Ø After declaring a variable, it can be
reused throughout the code.
Ø Assignment Operator (=) is used to
assign the value to a variable.
v
Syntax of
declaring a variable in PHP is given below:
$variablename=value;
v
Rules for
declaring PHP variable:
Ø A variable must start with a dollar
($) sign, followed by the variable name.
Ø It can only contain alpha-numeric
character and underscore (A-z, 0-9, _).
Ø A variable name must start with a
letter or underscore (_) character.
Ø A PHP variable name cannot contain
spaces.
Ø One thing to be kept in mind that the
variable name cannot start with a number or special symbols.
Ø PHP variables are case-sensitive, so
$name and $NAME both are treated as different variable.
PHP Variable: Declaring string, integer, and float
<?php
$str="hello string";
$x=200;
$y=44.6;
echo "string is: $str <br/>";
echo "integer is: $x <br/>";
echo "float is: $y <br/>";
?>
Output:
string is: hello string
integer is: 200
float is: 44.6
PHP Variable Scope
v
The
scope of a variable is defined as its range in the program under which it can
be accessed.
v
PHP
has three types of variable scopes:
Ø
Local variable
Ø
Global variable
Ø
Static variable
Local
variable
v
The
variables that are declared within a function are called local variables for
that function.
v
These
local variables have their scope only in that particular function in which they
are declared.
v
This
means that these variables cannot be accessed outside the function, as they
have local scope.
v
A
variable declaration outside the function with the same name is completely
different from the variable declared inside the function.
File: local_variable1.php
<?php
function local_var()
{
$num = 45; //local variable
echo "Local variable declared inside the function is: ". $num;
}
local_var();
?>
Output:
Local variable declared inside the function is: 45
Notice: Undefined variable: lang in D:\xampp\htdocs\program\p3.php on line 28
Global
variable
v
The
global variables are the variables that are declared outside the function.
v
These
variables can be accessed anywhere in the program.
v
To
access the global variable within a function, use the GLOBAL keyword before the
variable.
v
However,
these variables can be directly accessed or used outside the function without
any keyword.
v
Therefore
there is no need to use any keyword to access a global variable outside the
function.
Example:
File: global_variable1.php
<?php
$name = "Sanaya Sharma"; //Global Variable
function global_var()
{
global $name;
echo "Variable inside the function: ". $name;
echo "</br>";
}
global_var();
echo "Variable outside the function: ". $name;
?>
Output:
Variable inside the function: Sanaya Sharma
Variable outside the function: Sanaya SharmaNote:
Static
variable
v
It
is a feature of PHP to delete the variable, once it completes its execution and
memory is freed.
v
Sometimes
we need to store a variable even after completion of function execution.
v
Therefore,
another important feature of variable scoping is static variable.
v
We
use the static keyword before the variable to define a variable, and this
variable is called as static
variable.
Static variables exist only
in a local function, but it does not free its memory after the program
execution leaves the scope.
Example:
File: static_variable.php
<?php
function static_var()
{
static $num1 = 3; //static variable
$num2 = 6; //Non-static variable
//increment in non-static variable
$num1++;
//increment in static variable
$num2++;
echo "Static: " .$num1 ."</br>";
echo "Non-static: " .$num2 ."</br>";
} //first function call
static_var();
//second function call
static_var();
?>
Output:
Static: 4
Non-static: 7
Static: 5
Non-static: 7
Comments
Post a Comment