What is a PHP variable?
A variable is a container for storing information;
Second, the grammatical format
$ variable name = 1;
Three, PHP variable rules
1. The variable begins with the $ symbol, followed by the name of the variable;
2. The variable name must begin with a letter or an underscore;
3, variable name can not start with the number;
4. Variable names can only contain letters, numbers, characters, and underscores (A-Z, 0-9, and _)
5, variable name sensitivity to case ($y and $Y are two different variables)
6, Notice
For example, you enter the following code in PHP:
<?php
$a = $b = $c =wode;
$d = $b + $c;
Echo $d;
Entering "localhost" in the browser will eject an error as shown in the following illustration.
This error resolution:
First step: Open the configuration file in Phpstudy
Step Two: Search the error_reporting in the document with the shortcut key (ctrl+f), and put it behind E_all | E_strict changed to e_all& ' E_notice.
Step three: Restart the Phpstudy, the error message will not be displayed after refreshing the browser.
7, the variable can be declared together, said, as follows;
$a = $b = $c = 2;
echo $a, $b, $c;
Four, PHP variable example description
<?php
$x = 5;
$y = 6;
$z = $x + $y;
Echo $z;
?>
In the above example, $x = 3; $y = 4; $z = $x + $y; Are all used in the syntax of variables, the above variables are similar to algebra, as follows.
X=5 y=6 Z=x+y
In algebra we use letters (such as x) to hold values (such as 5).
From the expression z=x+y above, we can calculate that the value of Z is 11.
In PHP, these three letters (X,Y,Z) are called variables.
V. Scope of PHP variables
PHP has four different scope of variables:
Local
Global
Static
Parameter
Local and global scopes
A variable declared outside of a function owns the Global scope and can only be accessed outside of the function.
Variables declared within a function have a local scope and can only be accessed within a function.
The following example tests variables with local and global scope:
<?php
$x = 5; Global variables
function MyTest ()
{
$y =10;//Local Variables
echo "<p> test variables are:<p> inside functions";
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";
?>
To call a global variable defined outside of a function within a function, we need to add the global keyword before the variable in the function:
<?php
$x = 5;
$y = 10;
function MyTest ()
{
Global $x, $y;
$y = $x + $y;
}
MyTest ();
echo $y;//Output 15
?>
PHP stores all global variables in an array named $GLOBALS [index].
The name of the index save variable.
This array can be accessed within the function, or it can be used directly to update global variables.
<?php
$x = 5;
$y = 10;
function MyTest ()
{
$GLOBALS [' y ']= $GLOBALS [' x ']+ $GLOBALS [' Y '];
}
MyTest ();
Echo $y;
?>
Static Scope
When a function completes, all its variables are usually deleted. However, sometimes you want a local variable not to be deleted, you can use the static keyword
<?php
function MyTest ()
{
static $x = 0;
echo $x;
$x + +;
}
MyTest ();
MyTest ();
MyTest ();
?>
Parameter scopes
A parameter is a local variable that passes a value to a function by calling code.
Parameters are declared in the parameter list as part of the function declaration:
<?php
function MyTest ($x)
{
echo $x;
}
MyTest (5);
?>
The above is the entire content of PHP variables, I hope to help you learn.