PHP variables
Variablesare "containers" used to store information:
Everyone is in junior high school time. Teachers often teach you this.
Excuse me, Li Lei and Han Meimei, if:
x = 5
y = 6
then x + y equals how much? Everyone will answer without hesitation. x + y equals 11.
Next let’s look at the following junior high school mathematics knowledge. What is the result of x + y?
x = 5
y = 6
x = 8
I guess everyone will do the same The unhesitating answer: The result of x + y is 14.
This is a variable!
Several characteristics of variables:
1. x = 5 Assign the value 5 on the right to x
2 on the left. The second section x = 8, and the final result of x + y is equal to 14, indicating that x is in the operation (execution) from top to bottom. Can be reassigned.
PHP Variables
Our variables in PHP is also like this. But there are a fewFeatures:
1. It must start with$. For example, the variable , Chinese, _ is not a special symbol
5. Variable names must be meaningful (don’t write variable names like xxx, aaa, ccc)
6. $ is called the dollar sign, an English word :dollar. PHP variables must start with a dollar sign. Picture showing how “money” it is to do PHP
##Create (declare) PHP variables
PHP has no command to declare variables.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.
PHP is a weakly typed language
In the above example , we notice that it is not necessary to declare the data type of this 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
·##Local and global scope Local and global scope (local global)
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.
Example
Variables declared inside a PHP function are local variables and can only be used inside the function Visit:
测试函数内变量: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, and the $y variable is declared inside the function, so it is a"; echo "变量 x 为: $x"; //输出错误 Notice: Undefined variable: echo "
"; echo "变量 y 为: $y"; } myTest(); echo "测试函数外变量:
"; echo "变量 x 为: $x"; echo "
"; echo "变量 y 为: $y"; //输出错误 Notice: Undefined variable:
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 determined outside the function
meaning, cannot be used within a 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 in the function
Definition, belongs to local variables.
NoteYou can use the same variable name in different functions, because the variable names defined in these functions are local variables and only act within that function.
PHP global keyword
globalKeywords are used to access global variables within functions.
Example
#To call a global variable defined outside a function within a function, we need to add the global keyword before the variable in the function:
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, and can also be used directly to update global variables.
The above example can be written like this:
Static scope
#When a function completes, all of 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:
Example
Then, each time the function is called, the variable will retain the value when the function was called before.: This variable is still a local variable of the function.
Note
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:
Example
?php function myTest($x) { echo $x; } myTest(name); ?>We will discuss this in more detail in the PHP Functions chapter.