2. What is a
variable?
A variable in PHP is the name of the
memory location that holds data. In
PHP, a variable is declared using the
$ sign followed by the variable name.
The main way to store information in
the middle of a PHP program is by
using a variable.
2
3. PHPVariable
Rules
• 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.
3
4. Types of
Variable
Data types utilized by PHP to declare or
build variables:
• Null
• Doubles
• Integers
• Strings
• Booleans
• Resources
• Objects
• Arrays
4
5. Assignment
A variable is said to be assigned a value in
an assignment statement. E.g.
$NumberHours = 45;
$WorkerFirstName = “Pete”;
The assignment statement should not be
thought of as expressing mathematical
equality rather it a set of instructions to
evaluate the expression on the right hand
side and give the variable on the left hand
side the value that results. Thus
$Counter = $Counter + 1;
Makes sense as an assignment
statement even though it makes no
sense as mathematics.
5
6. PHP Variables
Scopes PHPVariables Scope In PHP, variables can be
declared anywhere in the script.The scope of
a variable is the part of the script where the
variable can be referenced/used.
PHP has three different variable scopes:
• Local
• Global
• Static
6
7. Local
Variable
The variables that are declared within a function are called
local variables for that function. These local variables have
their scope only in that particular function in which they
are declared. This means that these variables cannot be
accessed outside the function, as they have local scope.
Example:
<?php
function local_var()
{
$num = 45; //local variable
echo "Local variable declared inside the function is: ". $
num;
}
local_var();
?>
7
8. Global
Variable
The global variables are the variables that are
declared outside the function. These variables
can be accessed anywhere in the program. To
access the global variable within a function, use
the GLOBAL keyword before the variable.
However, these variables can be directly
accessed or used outside the function without
any keyword. Therefore there is no need to use
any keyword to access a global variable
outside the function.
Ex:<?php
$name = “VK"; //global variable
function global_var()
{
echo "Variable inside the function: ". $name;
echo "</br>";
}
global_var();
?>
8
9. Static
Variable
A static variable is the attribute of PHP to erase
the variable once it finishes its execution and
the memory is liberated. However, in some
cases, we really want to store the variables
even after the fulfillment of function execution.
Ex:
<?php
function keep_track() {
STATIC $count = 0;
$count++;
print $count;
print "<br/>";
}
keep_track();
keep_track();
keep_track();
?>
9