Variables in PHP development basic tutorial
Variables are "containers" used to store information:
See example below
php.cn
1. Similar to algebra
x=5
y=6
z=x+y
In algebra we use letters (like x) to hold values (like 5).
From the above expression z=x+y, we can calculate that the value of z is 11.
In PHP, these three letters are called variables.
Note: Please think of variables as containers for storing data.
2. PHP variables
Just like algebra, PHP variables can be used to save values (x=5) and expressions (z =x+y).
Variable names can be short (such as x and y) or more descriptive (such as number, total_volume).
3. PHP variable rules
Variables start with the $ symbol, followed by the name of the variable
Variable names must start with letters or underscores
Variable names cannot start with numbers
Variable names can only contain letters Numeric characters and underscores (A-z, 0-9, and _)
Variable names are case-sensitive ($y and $Y are two different variables)
Note: PHP variable names are case-sensitive!
Example:
4. Create PHP variables
PHP No declaration Variable command.
The variable is created when you first assign a value to it:
In the execution of the above statement, the variable txt will hold the value Hello 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.
5. PHP is a loosely typed language
In the above example, we noticed that it is not necessary to ask PHP Declare the data type of the variable.
PHP will automatically convert the variable to 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.
6. PHP variable scope (it is recommended for beginners to understand it temporarily, without going into details)
The scope of variables is in the script The part where the variable can be referenced/used.
PHP has four different variable scopes:
local
global
static
parameter
##1. Local and global scope
Variables defined outside all functions 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 a PHP function are local variables and can only be accessed inside the function:
php.cn 测试函数内变量:"; echo "变量 x 为: $x"; echo "
"; echo "变量 y 为: $y"; } myTest(); echo "测试函数外变量:
"; echo "变量 x 为: $x"; echo "
"; echo "变量 y 为: $y"; ?>
2.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:
php.cn
Note: You can remove global to see the effect
PHP stores all global variables in an array named $GLOBALS[index]. index holds 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:
php.cn
Note: The actual effect of the two methods is the same
##3 .PHP static keyword
Note: Will the contents within global also be destroyed? ? ?php.cn
4. Parameter scope
Parameters are local variables that pass values to the function through the calling code. Parameters are declared in the parameter list as part of the function declaration:For more details, please see the PHP Functions chapter
php.cn
7. Variable variables
Learning experience:
#Understanding of the concept of variables, variables are containers for information
The scope and difference between the four scopes of variables