This article summarizes PHP variables and scopes
【Content List】
(1) PHP variable rules
(2) Create/declare variables
(3) Weakly typed languages
(4) Variable scope
(5) Scope
①Local and global scope; ②global keyword; ③Static scope; ④Parameter scope
【Details】
(1) PHP variable rules:
①Variables start with $;
②Case-sensitive, $a and $A are two different variables;
③ must start with a letter or underscore;
④Cannot contain spaces, only alphanumeric characters and underscores (Az, 0-9 and _ )
(2) Create/declare variables:
PHP has no command for declaring variables, variables are created the first time you assign to it
Note : When assigning a text value to a variable, please put quotation marks around the text value, for example $txt="Hello world!";
(3) Weakly typed languages
When creating/declaring a variable, it is not necessary to declare the data type of the variable to PHP. PHP will automatically convert the variable to the correct data type based on the value of the variable
In strongly typed programming languages, we have to declare (define) the type and name of a variable before using it
(4) 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 local, global global, static static, parameter parameter scope
(5) Scope
① Local and global scope
Variables defined outside all functions have global scope. Except functions, global variables can be accessed by any part of the script. To access a global variable within a function, use the global keyword.
Variables declared inside a PHP function are local variables and can only be accessed inside the function:
<?php $x=5; // global variable function myTest() { $y=10; // local variable echo "<p>The variable in the test 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"; ?>
When we call the myTest() function and output the values of the two variables, the function will output the value of the local variable $y, but not the value of $x, because the $x variable is defined outside the function and cannot be used inside the function, if To access a global variable within a function, use the global keyword.
Note: You can use the same variable name in different functions, because the variable names defined in these functions are local variables and only work within the function
②global keyword
It is used to access global variables within the function and call the global variables defined outside the function within the function. We need to add the global keyword before the variables in the function
<?php $x=5; $y=10; function myTest(){ overall $x,$y; $y=$x+$y; } myTest(); echo $y; // output 15 ?>
PHP stores all global variables in an array called $GLOBALS[index]. index holds the name of the variable. This array can be accessed inside a function or used directly to update global variables.
The above example can be written as:
<?php $x=5; $y=10; function myTest(){ $ GLOBALS ['y'] = $ GLOBALS ['x'] + $ GLOBALS ['y']; } myTest(); echo $y; ?>
Regarding the difference and usage of global and $GLOBALS[''], I will summarize in the following article
③Static scope
When a function completes, all of its variables are usually deleted. However, sometimes you want a local variable not to be deleted.
Then, each time the function is called, the variable will hold the value from the last time the function was called.
To do this, use the static keyword when declaring the variable for the first time:
<?php function test(){ static $x = 5; echo $x; $x++; } test(); test(); test(); ?>
Note: the variable is still local to the function
④Parameter scope
Parameters are local variables that pass values to the function by calling code, and parameters are declared in the parameter list as part of the function declaration:
<?php function myTest($x){ echo $x; } myTest(568); ?>
.