1. Differences between common variables and global variables first, PHP is a LooselyTypedLanguage ). You do not need to declare the variable before setting it. $ Abc123asdasd; echobr; echo $ abc; echobr; var_dump ($ abc); $ abc234; echobr; echo $ abc; var_dump ($ abc); output you
1. Differences between common variables and global variables first, PHP is a Loosely Typed Language ). You do not need to declare the variable before setting it. $ Abc = 123 asdasdasd; echo br; echo $ abc; echo br/; var_dump ($ abc); $ abc = 234; echo br; echo $ abc; var_dump ($ abc); output you
1. Differences between common variables and global variables
First, PHP is a Loosely Typed Language ).
You do not need to declare the variable before setting it.
$abc= "123asdasdasd";echo "
";echo $abc;echo "
";var_dump($abc);$abc = 234;echo "
";echo $abc;var_dump($abc);
The output, you know, is 123 and 234. variables are like a box. We put things in a box named abc. The box can only have one data type at the same time.
Question: The above $ abc = "123 asdasdasd"; isn't there two types of data: 123 and asdsdasd?
No, we use var_dump to print and find that it is of the string type, that is, the string type, because any variable content in php that is enclosed by single or double quotation marks will be treated as strings for processing.
The following $ abc = 123; is considered as a number.
During the development process, if you need to insert code in the middle, you should note that when naming, you should see that the previous Code does not have this name, and it is useless later. If yes, the same name cannot be used.
About global
The output in the above example is 3.
What if I comment out global $ a and $ B above?
See the following example.
In this example, 2 is output.
Why?
This is because $ a = 1, $ B = 2; here, only local variables are declared, and methods in vivo and in vitro are two different scopes.
Therefore, $ B in the second example is irrelevant to the method body and can be considered as two different variables. So the result is 2. however, in the first example, after we declare $ a and $ B globally, the variables can be used in the method body and in vitro. The sum operation is $ a = 1, $ B = 2, so the final result is 3. In my opinion, using global to declare variables in the method body can also be seen as a process of passing values. Because if there is another function under sum that needs to be called $ a, $ B needs to be global again
If you do not understand the scope, you can look at the following example.
";echo $b;?>
5 and 2 are output here.