Variables in PHP development basic tutorial
Variables are "containers" used to store information:
See example below
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php $x=5; $y=6; $z=$x+$y; echo $z; ?> </body> </html>
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:
<?php //site = 'Hello'; // 非法变量名;以数字开头 $_4site = 'World'; // 合法变量名;以下划线开头 $i小明is = 'haha'; // 合法变量名;可以用中文 ?>
4. Create PHP variables
PHP No declaration Variable command.
The variable is created when you first assign a value to it:
<?php $txt="Hello world!"; $x=5; $y=10.5; ?>
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:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?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"; ?> </body> </html>
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:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php $x=5; $y=6; function test(){ global $x,$y; $y=$x+$y; } test(); echo $y; ?> </body> </html>
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:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php $x=5; $y=6; function myTest() { $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y']; } myTest(); myTest(); myTest(); myTest(); echo $y; ?> </body> </html>
Note: The actual effect of the two methods is the same
##3 .PHP static keyword
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php function myTest() { static $x=0; echo $x; $x++; } myTest(); myTest(); myTest(); ?> </body> </html>Note: Will the contents within global also be destroyed? ? ?
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
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php function test($x) { echo $x; } test(2016); ?> </body> </html>
7. Variable variables
<?php $x= 'hello'; $$x='xiao ming'; echo "$x ${$x}"; ?>The above code can also be written as:
<?php $x= 'hello'; $hello='xiao ming'; echo "$x $hello"; ?>These two examples are equivalentNote: Variable variables are used in arrays and must solve an ambiguous problem. This is when writing $$a[1], the parser needs to know whether it wants $a[1] as a variable, or whether it wants $$a as a variable and extracts the variable with index [1] value. The syntax to solve this problem is to use ${$a[1]} for the first case and ${$a}[1] for the second case.
Learning experience:
#Understanding of the concept of variables, variables are containers for information
The scope and difference between the four scopes of variables