Summary of PHP variables recommended for beginners

Source: Internet
Author: User

PHP does not need to display declaration variables. Variable declaration can be performed at the same time as assignment.Good Programming habits: all variables should be declared before use, preferably with annotations.

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, modifying any of them will reflect the rest of the variables.
Example:
<? Php
$ 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 declared variable in the function is considered a local variable and can only be referenced in the function. When the declared variable function 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. <br> ";
}
Assignx ();
Print "\ $ x outside of function is $ x. <br> ";
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, they cannot be accessed 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
Unlike local variables, global variables can be accessed anywhere in the program. When changing a GLOBAL variable in a function, you need to display the life of the variable as a GLOBAL variable in this function, as long as you 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, but static variables do not lose the value when the function exits, and the value can be retained when the function is called again. You can declare a STATIC variable by adding the keyword STATIC before the worker beam.
STATIC $ somevar;
Consider an example:
Function keep_track (){
STATIC $ count = 0;
$ Count ++;
Print $ count;
Print "<br> ";
}
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 many useful pre-defined variables that can be accessed at the person and location where the script is executed and 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, while 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 <br> ";
}
Show the user's IP Address:
Print "HI! Your IP address is ". $ _ SERVER ['remote_addr '];

To use a predefined variable array in PHP, you must enable the configuration parameter track_vars in the PHP. ini file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.