PHP Variables
Variables are containers for storing information:
Example
<?php
$x=5;
$y=6;
$z=$x+$y;
echo $z;
?>
Similar to Algebra
x=5
In algebra, we use letters (like x) and give them values (like 5).
From the expression z=x+y, we can calculate that the value of z is 11.
In PHP, these letters are called variables.
| | Variables are containers for storing data. | | --- | --- |
PHP Variables
Similar to algebra, PHP variables can be assigned a value (x=5) or an expression (z=x+y).
Variables can be short names (like x and y) or more descriptive names (like age, carname, totalvolume).
PHP variable rules:
- Variables start with the $ sign, followed by the name of the variable
- Variable names must start with a letter or an underscore character
- Variable names can only contain alpha-numeric characters and underscores (A-z, 0-9, and _)
- Variable names cannot contain spaces
- Variable names are case-sensitive ($y and $Y are two different variables)
| | Both PHP statements and PHP variables are case-sensitive. | | --- | --- |
Creating (Declaring) PHP Variables
PHP does not require a command to declare a variable.
A variable is created the moment you first assign a value to it:
Example
<?php
$txt="Hello world!";
$x=5;
$y=10.5;
?>
In the above statement, the variable txt will store the value Hello world!, and the variable x will store the value 5.
Note: When you assign a text value to a variable, put quotes around the value.
PHP is a Loosely Typed Language
In the above examples, we notice that we do not have to declare the data type of the variable.
PHP automatically converts the variable to the correct data type, depending on its value.
In strongly typed programming languages, we must declare the type and name of the variable before using it.
PHP Variable Scope
The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has four different variable scopes:
- local
- global
- static
- parameter
Local and Global Scope
A variable declared outside a function has a global scope and can only be accessed outside a function. To access a global variable inside a function, use the global keyword.
A variable declared within a function has a local scope and can only be accessed within that function:
Example
<?php
$x=5; // global variable
function myTest()
{
$y=10; // local variable
echo "<p>Test variables inside the function:<p>";
echo "Variable x is: $x";
echo "<br>";
echo "Variable y is: $y";
}
myTest();
echo "<p>Test variables outside the function:<p>";
echo "Variable x is: $x";
echo "<br>";
echo "Variable y is: $y";
?>
In the above example, the myTest() function defines the variables $x and $y. $x is declared outside the function, so it is a global variable, while $y is declared inside the function, making it a local variable.
When we call the myTest() function and output the values of both variables, the function will output the local variable $y's value but will not output $x's value because $x is defined outside the function and cannot be used inside it. To access a global variable inside a function, use the global keyword.
Then, when we output the values of both variables outside the myTest() function, the function will output the global variable $x's value but will not output $y's value because $y is defined inside the function and is local to it.
| | You can use the same variable names in different functions because variables defined in one function are local and only valid within that function. | | --- | --- |
PHP global Keyword
The global keyword is used to access global variables from within a function.
To access a global variable inside a function, use the global keyword before the variable:
Example
<?php
$x=5;
$y=10;
function myTest()
{
global $x,$y;
$y=$x+$y;
}
myTest();
echo $y; // outputs 15
?>
PHP stores all global variables in an array called $GLOBALS[index]. index holds the name of the variable. This array can be accessed within functions and can be used to update global variables directly.
The above example can be rewritten like this:
Example
<?php
$x=5;
$y=10;
function myTest()
{
$GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y'];
}
myTest();
echo $y;
?>
Static Scope
When a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable not to be deleted. We need it for a further job.
To do this, use the static keyword when you first declare the variable:
Example
<?php
function myTest()
{
static $x=0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.
Note: The variable is still local to the function. When a function completes, all of its variables are usually deleted. However, there are times when you want a local variable not to be deleted.
To achieve this, use the static keyword when you first declare the variable:
Example
<?php
function myTest()
{
static $x=0;
echo $x;
$x++;
echo PHP_EOL; // Line break
}
myTest();
myTest();
myTest();
?>
Then, each time the function is called, the variable will retain the value from the previous call of the function.
Note: The variable is still a local variable of the function.
Parameter Scope
Parameters are local variables whose values are passed to the function by the calling code.
Parameters are declared in the parameter list as part of the function declaration:
Example
<?php
function myTest($x)
{
echo $x;
}
myTest(5);
?>
We will discuss this in more detail in the PHP Functions chapter.