PHP variables
What are PHP variables?
1. Variables in PHP are containers used to store information, similar to sets in mathematics.
2. The assignment method is similar to algebra in mathematics.
Variables are used to store values. When we order the server to work, we often need to generate some data, which needs to be temporarily stored for easy access. We can also understand that a variable is like a shopping bag. We can use it to hold apples and durians (of course it can also be used to hold roses). It should be noted that in general, a variable can only hold one (don’t be too greedy) Value, unless it is a composite variable (variable types will be introduced later), when we put in an apple and then put in durian, the apple will be replaced, and then put in a banana, then the durian will be replaced.
Variables are "containers" used to store information:
Instance
<?php $x=5; $y=6; $z=$x+$y; echo $z; ?>
Running Instance»
Click the "Run Example" button to view the online example
Similar to algebra
x=5
y=6
z=x +y
In algebra, we use letters (like x) and assign values to them (like 5).
From the above expression z=x+y, we can calculate the value of z to be 11.
In PHP, these letters are called variables.
Variables are containers used to store data. |
PHP variables
Similar to algebra, PHP variables can be assigned a value (x=5) or an expression ( z=x+y).
Variables can have very short names (such as x and y) or more descriptive names (such as age, carname, totalvolume).
PHP variable rules:
Variables start with the $ symbol, followed by the name of the variable
Variable names must start with letters Or start with an underscore character
Variable names can only contain alphanumeric characters and underscores (A-z, 0-9 and _)
Variable names cannot Contains spaces
Variable names are case-sensitive ($y and $Y are two different variables)
PHP statements and PHP variables are case-sensitive. |
Create (declare) PHP variables
PHP has no command to declare variables.
A variable is created the first time you assign a value to it:
Instance
<?php $txt="Hello world!"; $x=5; $y=10.5; echo $txt; ?>
Running Instance»
Click the "Run Example" button to view the online example
In the execution of the above statement, the variable txt will save the valueHello world! , and the variable x will hold the value 5.
Note: When you assign a text value to a variable, please add quotation marks around the text value.
PHP is a weakly typed language
In the above example, we noticed that it is not necessary to declare the data type of the variable to PHP.
PHP will automatically convert the variable into the correct data type based on its value.
In a strongly typed programming language, we must declare (define) 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
Defined outside all functions Variables have global scope. In addition to functions, global variables can be accessed by any part of the script. To access a global variable in a function, you need to use the global keyword.
Variables declared inside PHP functions are local variables and can only be accessed inside the function:
Example
<?php $x=5; // 全局变量 function myTest() { $y=10; // 局部变量 echo "<p>测试函数内变量:<p>"; echo "变量 x 为: $x"; echo "<br>"; echo "变量 y 为: $y"; } myTest(); echo "<p>测试函数外变量:<p>"; echo "变量 x 为: $x"; echo "<br>"; echo "变量 y 为: $y"; ?>
Run Example»
Click the "Run Example" button to view the online example
In the above example, the myTest() function defines the $x and $y variables. The $x variable is declared outside the function, so it is a global variable , the $y variable is declared inside the function so it is a local variable.
When we call the myTest() function and output the values of two variables, The function will output the value of the local variable $y, but cannot output the value of $x, because the $x variable is defined outside the function and cannot be used within the function. If you want to access a global variable in a function, you need to use the global keyword .
Then we output the values of the two variables outside the myTest() function. The function will output the value of all local variables $x, but cannot output the value of $y because the $y variable is defined in the function. Belongs to local variables.
You can use the same variable name in different functions, because the variable names defined in these functions are local variables and only affect within this function. |
PHP global keyword
The global keyword is used to access global variables within a function.
To call global variables defined outside the function within a function, we need to add the global keyword before the variables in the function:
Example
<?php $x=5; $y=10; function myTest() { global $x,$y; $y=$x+$y; } myTest(); echo $y; ?>
Run Example»
Click the "Run Example" button to view the online example
PHP stores all global variables in a file called $GLOBALS[index] in the array. index Save the name of the variable. This array can be accessed inside the function or used directly to update global variables.
The above example can be written like this:
Instance
<?php $x=5; $y=10; function myTest() { $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y']; } myTest(); echo $y; ?>
Running Example»
Click the "Run Instance" button to view the online instance
Note: The output value of the above code in the PHP5 version is 15; however, the PHP Chinese website online instance environment is the PHP7 version, so the output value Should be 10. Because in the latest php7 version, the PHPglobal variable processing mechanism has been modified. In this version, the global keyword can only refer to simple variables. For a detailed introduction to the differences, please see this article:
What are the changes in the global variable mechanism in PHP5 and PHP7 versions? (Code test)
Static scope
When a function completes, all its variables are usually deleted. However, sometimes you want a local variable not to be deleted.
To do this, use the static keyword the first time you declare the variable:
Instance
<?php function myTest() { static $x=0; echo $x; $x++; } myTest(); myTest(); myTest(); ?>
Run instance»
Click the "Run instance" button to view the online instance
Then, each time the function is called, the variable will be retained The value when the function was last called.
Note: This variable is still a local variable of the function.
Parameter scope
Parameters are local variables whose values are passed to the function through the calling code.
Parameters are declared in the parameter list, as part of the function declaration:
Instance
<?php function myTest($x) { echo $x; } myTest(5); ?>
Running Instance»
Click the "Run Example" button to view the online example
We will discuss it in more detail in the PHP Function chapter.