A variable is a symbol of a variable. PHP variables can store different values at different times.
What is PHP Variable?
Variables are “containers” for storing information. A variable is a symbol that may store different values at different times. A variable is a named memory location. Variables contain data and can be used throughout program execution.
- In PHP, it does not need to be declared a variable before adding a value. PHP automatically transmits the correct data type, depending on its value.
- After declaring a variable it can be reused across the code.
- An assignment operator (
=
) is used to earmark the value of a variable.
PHP can be declared as variable:
$variable_name
=
value
;
Variable Declaration
Here’re some important Rules for PHP Variable Declaration:
- A variable always starts with a dollar ‘
$
‘ sign, followed by of the variable name. - A variable name can’t begin with a number.
- A variable name can begin with either a letter or an underscore character.
- A variable name may only contain alpha-numeric characters and underscores (
A-z
,0-9
and_
). - Variable names are case-sensitive.
The following are all valid variables:
- $variable
- $variable_name
- $_variable_name
- $model
Note that variables are case-sensitive. For instance, the following variables bear no relation to one another:
- $variable
- $Variable
- $VARIABLE
Variable Value Assignment
Assignment by value just comprises copying the value of the assigned appearance to the variable assignee. This is the usual basic type of assignment. Follow the examples:
The following example will show how the display text and a variable:
Code Example:
<!DOCTYPE html>
<html>
<head>
<title>Text Value Assignment</title>
</head>
<body>
<?php
$text = "CoderWell.com";
echo "I love $text!";
?>
</body>
</html>
This returns the following:
I love CoderWell.com!
The following example will show how the display sum of the two variable:
Code Example:
<!DOCTYPE html>
<html>
<head>
<title>Value Assignment</title>
</head>
<body>
<?php
$x = 3;
$y = 3;
echo $x + $y;
?>
</body>
</html>
This returns the following:
6
Assign Variables by Reference
You can create a variable that refers to the equal content as any other variable. Therefore, specifying a specific item of variables that any variable change will be reflected in all other variables mentioned in the same content. You can allocate variables by reference by associating an ampersand (&
) symbol. Let’s consider an example:
Code Example:
<?php
$name1 = "Code";
$name2 =& $name1;
echo $name1."<br>";
echo $name2."<br>";
// $name1 and $name2 both equal "Code"
$name2 = "Grope";
echo $name1."<br>";
echo $name2."<br>";
// $name1 and $name2 both equal "Grope"
?>
This returns the following:
Code
Code
Grope
Grope
A variant reference-assignment syntax is also supported, which adds an ampersand to the referenced variables. The following examples follow this new syntax:
Code Example:
<?php
$name1 = "Code";
$name2 = &$name1;
echo $name1."<br>";
echo $name2."<br>";
// $name1 and $name2 both equal "Code"
$name2 = "Grope";
echo $name1."<br>";
echo $name2."<br>";
// $name1 and $name2 both equal "Grope"
?>
This returns the following:
Code
Code
Grope
Grope
Variable Scope
variables can declare them anywhere in a PHP script. The scope of a variable is the part of the script in which the variable may be referenced/used.
PHP variables can be one of the four scope types:
- Local variables
- Function parameters
- Global variables
- Static variables
Local Variables
A function is declared a variable local declared. That is, it can only be mentioned in that function. One of the functions that perform any functions outside of functions will be considered to be a completely different variable from one. Note that when you exit a function where a local variable is declared, that variable and its corresponding value are lost.
Code Example:
<?php
$xy = 8;
function xyz () {
$xy = 2;
printf("\$xy inside function is %d <br />", $xy);
}
xyz();
printf("\$xy outside of function is %d <br />", $xy);
?>
Executing this listing results in the following:
$xy inside function is 2
$xy outside of function is 8
You can see two different values for $xy output. This is because the $xy xyz() is located inside the local locale. To change the value of local $xy, there is no balance of any value outside the function. In the same note, changing the $xy located outside of the function has no effect on any variables in xyz().
Function Parameters
The function that receives the arguments must declare those arguments in the function headers. Although those arguments accept the value of the function outside, the function is no longer accessible after it is exited.
Function parameters are declared after function names and inner brackets. A few examples follow:
Code Example:
<?php
// multiply a value by 10 and return it to the caller
function x($value) {
$value = $value * 10;
return $value;
}
echo x(5)."<br>";
echo x(100);
?>
Executing this listing results in the following:
50
100
Global Variables
In contrast to local variables, a global variable can be accessed in any part of the program. PHP Global Variable Declaration is very easy, if we want a variable accessible from a function, we can use Global Keyword.
Whether global variables can be accessed anywhere in our scripts, inside or outside a function In order to correct a global variable, it must be explicitly declared global in the function where it will be fixed. Follow the examples:
Code Example:
<?php
$value = 10;
function addition() {
global $value;
$value++;
echo "Some are $value";
}
addition();
?>
Executing this listing results in the following:
Some are 11
The displayed value of $value
would be 11
. However, if you were to omit this line, global $value
; then the variable $value
would be assigned the value 1
because $value
would then be considered local within the addition()
function. This local declaration will definitely be set to 0 then the value will be increased by 1 to display 1.
An alternative way to declare a variable as global is to use PHP’s global array. Reconsidering the preceding example, you can use this array to declare the variable $value
to be global:
Code Example:
<?php
$value = 10;
function addition() {
$GLOBALS["value"]++;
}
addition();
echo "Some are ".$GLOBALS["value"];
?>
This returns the following:
Some are 11
Static Variables
The final type of variable scoping to discuss is known as static. Reverse variables are declared as function parameters, which are lost at the exit of the function, a static variable does not lose its value when the function exits and is retained if the function is repeated. You can declare the character static as a variable with the name of the variable, like so: STATIC $varname
;
Follow the examples:
Code Example:
<?php
function static_var() {
static $count = 0;
$count++;
echo $count;
echo "
";
}
static_var();
static_var();
static_var();
?>
The outcome would be as follows:
1
2
3
The $count
is static, it retains its previous value every time the function is executed.