1. assign values to variables after they are declared. There are two ways to assign values: value assignment and reference assignment. 1. value assignment $ color & quot; red & quot; $ sum12 + & quot; 15 & quot; * $ sum
I. variable assignment
You can assign values to variables after they are declared. There are two methods: value assignment and reference assignment.
1. value assignment
$ Color = "red ";
$ Sum = 12 + "15";/* $ sum = 27 */
2. if you want two variables to point to the same copy of a value, you must assign values by referencing them.
Reference assignment
The created variable is the same as that referenced by another variable. if multiple variables reference the same content and modify any of the variables, the other variables will be reflected.
The PHP instance code is as follows:
-
- $ Value1 = "hello ";
- $ Value2 = & value1;/* $ value1 and $ value2 both equal "hello ".*/
- $ Value2 = "goodbye";/* $ value1 and $ value2 both equeal "goodbye ".*/
- ?>
II. Scope of variables
Variables can be declared at any position of the php script, but the location of the declared variables will greatly affect the scope of the access variables. the accessible scope is called scope ).
Scope of php variable 4:
△Local variable
△Function parameters
△Global variable
△Static variable
1. local variables
The variable declared in the function is considered a local variable and can only be referenced in the function. when the function that declares the variable is exited, the variable and the corresponding value are revoked, this eliminates the possibility that variables that cause global access are intentionally or unintentionally modified.
- $ X = 4;
- Function assignx (){
- $ X = 0;
- Print "$ x inside function is $ x.
";
- }
- Assignx ();
- Print "$ x outside of function is $ x.
";
The code execution result is:
$ X inside function is 0.
$ X outside function is 4.
2. function parameters
Like other programming languages, any function that accepts parameters must declare these parameters in the function header, although these parameters accept values outside the function, however, you cannot access these parameters after exiting the function. (except for parameters passed by reference)
For example:
- Function x10 ($ value ){
- $ Value = $ value * 10;
- Return $ value;
- }
After the function is executed, the parameter is about to be revoked.
3. global variables
In contrast to local variables, global variables can be accessed anywhere in the program. when a global variable is changed in the function, you need to display the variable as a global variable in this function, you only need to add global before the variable in the function.
For example:
- $ Somevar = 15;
- Function addit (){
- Global $ somevar;
- $ Somevar ++;
- Print "somevar is $ somevar ";
- }
- Addit ();
$ Somevar: The value displayed is 16. However, if global $ somevar is removed, the variable $ somevar is implicitly set to 0, and 1 is added. The value displayed at the end is 1.
Another way to declare global variables is to use the $ global array of php, as shown below:
- $ Somevar = 15;
- Function addit (){
- $ Globals ['somevar '] ++;
- }
- Addit ();
- Print "somevar is". $ globals ['somevar '];
- // Return value: somevar is 16.
4. static variables
Static scope: function parameters of common variables are revoked at the end of the function. However, static variables do not lose a value when the function exits and can be retained when the function is called again, you can declare a static variable by adding the keyword static to the front of the worker beam.
Static $ somevar;
Consider an example:
- Function keep_track (){
- Static $ count = 0;
- $ Count ++;
- Print $ count;
- Print"
";
- }
- Keep_track ();
- Keep_track ();
- Keep_track ();
- Keep_track ();
If $ count is not specified as static (corresponding, $ count is a local variable), the output will be
1
1
1
1
Because $ count is static, it retains the previous value each time the function is executed. the output is as follows:
1
2
3
4
Static scopes are useful for recursive functions. a recursive function is a powerful programming concept. it is a function that can call itself repeatedly until a certain condition is met.
5. php Super global variables
Php provides a lot of useful predefined variables that can be accessed at the person and location where the script is executed. it is used to provide a large amount of environment-related information, you can use these variables to obtain detailed information about the current user session, user operating system environment, and local operating environment. php creates some variables, the availability and value of many other variables depend on the operating system and web services.
Output all predefined variables:
- Foreach ($ _ server as $ var => $ value ){
- Echo "$ var => $ value
";
- }
Show the user's IP address:
Print "hi! Your ip address is ". $ _ server ['remote_addr '];