PHP declaration variables

Source: Internet
Author: User
PHP declaration variable 2.4

Variable refers to the amount of changes that can occur at any time during the running of the program. you can use only one variable or multiple variables in the code, variables can contain words, values, dates, and attributes. The value of the variable is temporary. when the program runs, the value exists. if the program ends, the value of the variable will be lost. Although variables are used in the previous example, they are not described in detail. This section describes how to create variables and how to reference variables.

2.4.1 create a variable

The variables in PHP use the dollar sign ($) as the prefix identifier. the identifier is a symbol that identifies different objects, such as the variable name and function name, or the name of another custom object. In PHP, the identifier name must comply with the following rules:

An identifier can contain one or more characters, but must begin with a letter or underscore. In addition, an identifier can only contain letters, numbers, underscores, and other ASCII characters ranging from 127 to 255. For example, the names of my_a, Ss, and _ value identifiers are legal, while the names of q ^ a and 4tt variables are invalid.

L The identifier is case sensitive. Therefore, the variable $ recipe is different from the variable $ Recipe, $ rEciPe, or $ recipE.

L The identifier can be of any length. This is advantageous because the programmer can accurately describe the purpose of the identifier through the identifier name.

L The identifier name cannot be the same as any predefined PHP keyword.

It is a good habit to declare a variable before assigning a value to the variable. PHP is a weak type language. when declaring a variable, you do not need to display the declared variable. the variable can store any type of value. in PHP, the variable performs a type check during runtime, in addition, we can replace the value of a variable with another value of different types. next we declare a variable, and let another value of different types replace the value of the variable, and then declare a variable without a value, the code is as follows:

$ What = "Yound Tang ";

$ What = 25;

$ Name;

In PHP, there are two ways to assign values to variables: value assignment and reference assignment. A value assignment directly copies a value to the variable through a value assignment expression, which overwrites the original value of the variable. If no value is assigned when the variable is declared, the behavior is the same as NULL. Assign a value when declaring a variable is a common method of assigning a value to a variable, as shown in the following example:

$ Name = "Tang Xiaoyang ";

$ Age = "23 ";

$ Sex = "male ";

Echo "Your name is:". $ name ."
";

Echo "your age is:". $ age ."
";

Echo "your gender is:". $ sex ."
";

Run the code section. the execution result is as follows:

Your name is: Tang Xiaoyang

Your age is: 23

Your gender is: Male

In PHP, declared variables can be directly assigned values or not assigned values. when you need to store values using variables, you can reference them to assign values, reference value assignment indicates that the created variable is the same as that referenced by another variable. Therefore, if multiple variables reference the same content and modify any of the variables, the other variables will be reflected. Add an & symbol after the equal sign to complete the reference assignment. The following is an example of a value assignment.

$ Value1 = "Hello World ";

$ Value2 = & $ value1;

$ Value2 = "GoodBye ";

Echo $ value1 ."
";

Echo $ value2 ."
";

In the above code, create a variable value1 with a value of "Hello World". In the following statement, the variable $ value2 uses a reference value assignment, that is to say, the value of value1 is assigned to value2. at this time, the two variables are a life-community. when one variable changes, the other variables will display the results. The execution results of this section of the code are as follows:

GoodBye

GoodBye

2.4.2 variable scope

The location of the declared variable determines the scope of the variable. the scope of the variable determines which parts of the program can access the variable, and those parts cannot access the variable. in PHP, the scope of variables can be divided into four categories: local variables, function parameters, global variables, and static variables. This section describes the scope of variables.

1. local variables

In a function, declare that a variable is the local variable of the function. that is to say, the variable can only be accessed by the function's internal members. the function's external members cannot access the variable and are invisible. By default, function members cannot access variables defined outside the function (global variables ). Sometimes local variables are useful because local variables can eliminate the possibility of a side effect, otherwise these side effects will cause the variables that can be accessed globally to be modified intentionally or unintentionally. The following is an example of using local variables, as shown in code 2.13.

Code 2.13 use local variables

$ Count = 10;

Function AddCount ()

{

$ Count = 100;

$ Count = $ count + $ count;

Echo $ count;

Echo "<br/> ";

}

AddCount ();

Echo $ count;

?>

Run code 2.13. the execution result is as follows:

200

10

According to the output result, this section of code outputs two different values. this is because the variable in the AddCount function is a local variable. modifying the value of a local variable does not affect any external values of the function, the variables in the function are discarded at the end of the program, so the global variable value is 10.

2. function parameters

In PHP, the function can accept the corresponding parameters. although these parameters accept values outside the function, they cannot be accessed after exiting the function. after the function is executed, the parameter value disappears, which is closely related to the execution of the function. Function parameters are declared in brackets behind the function. the following example uses this parameter to create a function, as shown in code 2.14.

Code 2.14 uses function parameters

Function EchoNum ($ age, $ class)

{

Echo "Your age is:". $ age. "<br/> ";

Echo "class:". $ class;

}

EchoNum (18, "Class 8, Class 5, computer technology and science department ");

?>

Run the code section. the execution result is as follows:

Your age is: 18

Class: Class 5, Level 08, computer technology and science department

Function parameters can also be called local variables, meaning that these parameters only work within the function and cannot be accessed outside the function. when the function execution ends, the variables are also revoked.

3. global variables

Global variables can be accessed anywhere in the entire PHP program. to modify a global variable, you must explicitly declare it as a global variable in the function that modifies the variable, it is very easy to declare a global variable in a function. you only need to use the global keyword to declare the global variable in the function. The following is an example of how to modify the global variable in the function, this example is shown in code 2.15.

Code 2.15 modify global variables in a function

Function AddNum ()

{

Global $ num;

$ Num = $ num + $ num;

Echo $ num;

}

$ Num = 100;

AddNum ();

?>

Run code 2.15. the execution result is as follows:

200

If the global variable is not added before $ num, it is considered a local variable. The value displayed on the page is 0. after adding global, you can modify the global variable, another way to declare a global variable is to use the $ GLOBALS array of PHP. the effect of using this array is the same as that of using global. The following is an example of using the $ GLOBALS array, this example is shown in code 2.16.

Code 2.16 uses the $ GLOBALS array

Function AddNum ()

{

$ GLOBALS ['num'] = $ GLOBALS ['num'] + $ GLOBALS ['num'];

Echo "the value is:". $ GLOBALS ['num'];

}

$ Num = 100;

AddNum ();

?>

Run code 2.16. the execution result is as follows:

The value is 200.

When using global variables, be sure to note that using global variables is prone to exceptions.

4. static variables

The value of a static variable remains unchanged between two called functions. a static variable is only declared in a local function domain. a static variable can be declared using the keyword static. Static variables do not lose a value when the function exits, and can be retained when the function is called again. The following is an example of using static variables, as shown in code 2.17.

Code 2.17 use static variables

Function keepNum ()

{

Static $ num = 0;

$ Num ++;

Echo "the current static variable value is:". $ num;

Echo "<br/> ";

}

$ Num = 10;

Echo "the value of the variable num is:". $ num ."
";

KeepNum ();

KeepNum ();

?>

Run code 2.17. the execution result is as follows:

The value of the variable num is: 10.

The current static variable value is: 1

The current static variable value is: 2

The execution result of code 2.17 indicates that the variable is a static variable in the function, so the previous value is retained during function execution.

2.3.3 variable

To create a variable, you need to assign a value to the variable. of course, you can also reference the value assignment. sometimes you need to use the content of the variable as the variable name, that is, the variable is stored in another variable. To define a variable, you only need to add a dollar sign before the variable. The following is an example code:

$ Name = "Yound ";

$ Name = "Tang ";

Echo $ name. $ name;

?>

Run the code on this end. the execution result is as follows:

Yound Tang

As shown in the preceding example, a variable obtains the value of a common variable as the variable name of the variable. It can be used as a variable. at this time, both variables are defined.

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.