PHP Declaration variables

Source: Internet
Author: User
2.4 Variables

A variable is a quantity that can change at any time while the program is running, can use only one variable in code, or can use multiple variables, which can hold words, values, dates, and attributes. The value of the variable is temporary, and when the program runs, the value is present, and if the program ends, the value of the variable is lost. Although variables are used in the previous example, there is no detailed description, this section details how to create variables and how to reference them.

2.4.1 Creating variables

Variables in PHP are identifiers prefixed with the dollar sign ($), which is a symbol that identifies different objects, such as the name of a variable, the name of a function, or the name of another user-defined object. In PHP, identifiers must be named in accordance with the following rules:

The l identifier can consist of one or more characters, but must begin with a letter or an underscore. In addition, identifiers can only consist of letters, numbers, underscore characters, and other ASCII characters from 127 to 255. such as My_a, Ss, _value These identifier names are legal, and Q^a, 4tt the names of these variables are illegal.

L identifiers are case-sensitive. Therefore, the variable $recipe differs from the variable $recipe, $rEciPe, or $recipe.

The l identifier can be any length. This is good because, in this way, the programmer can accurately describe the use of identifiers by identifier names.

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

In the process of creating a variable, it is a good practice to declare the variable before assigning a value to the variable. Because PHP is a weakly typed language, when declaring a variable, you do not need to display the declaration variable, the variable can hold any type of value, in PHP, the variable is at run time type check, and can replace the value of the variable with another different type of value, the following declaration a variable, And let another different type of value replace the value of the variable, and then declare a variable that does not have an assignment, the exact code is as follows:

$what = "Yound Tang";

$what = 25;

$name;

In PHP, there are two ways to assign a variable to a value, assigning values and assigning values to a reference. A value assignment is to copy a value directly to a variable through an assignment expression, overwriting the variable's original value and, if there is no assignment when declaring the variable, its behavior is the same as null. Assigning a value when declaring a variable is an assignment method of a commonly used 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 sex is:". $sex. "
”;

Execute the code, and the result is as follows:

Your name is: Tang Xiaoyang

Your age is: 23

Your sex is: male

In PHP, declaring variables can be assigned directly or not, and when you need to store a value with a variable, you can refer to the variable assignment, which means that the variable you created is the same as the content referenced by another variable. Therefore, if more than one variable references the same content, modifying either of these variables will be reflected on the remaining variables. A reference assignment can be done by adding a & symbol after the equals sign. An example form of a reference assignment is shown below.

$value 1= "Hello World";

$value 2=& $value 1;

$value 2= "GoodBye";

echo $value 1. "
”;

echo $value 2. "
”;

In the above code, create a variable value1 and assign the value "Hello World", in the following statement, the variable $value2 takes the reference assignment, that is, the value of value1 is assigned to the value2, at this time the two variables is a life community, when a change, The other one shows the result, and the result of the code execution is as follows:

GoodBye

GoodBye

2.4.2 Variable Scope

Declaring a variable's position determines the scope of the variable, and the scope of the variable determines which parts of the program can access the variable, and the scope of the variable can be divided into four classes in PHP: Local variables, function parameters, global variables, and static variables. This section focuses on the scope of these scopes for variables.

1. Local variables

Declaring a variable in a function is a local variable of that function, which means that the variable can only be accessed by members inside the function, and the external member of the function cannot access the variable and is not visible. By default, a member inside a function cannot access a variable defined outside the function (a global variable that is commonly referred to). Local variables can sometimes be useful because local variables eliminate the possibility of a side effect, otherwise these side effects will cause the globally accessible variable to be modified intentionally or unintentionally. The following creates an example that uses a local variable, as shown in code 2.13.

Code 2.13 using Local variables

$count = 10;

function Addcount ()

{

$count = 100;

$count = $count + $count;

Echo $count;

echo "<br/>";

}

Addcount ();

Echo $count;

?>

Execute code 2.13, and the result is as follows:

200

10

With the output, the code outputs two different values because the variables in the function Addcount function are local variables, and modifying the values of local variables does not affect any values outside the function, and the variables in the function are discarded at the end of the program, so the global variable value is still 10.

2. function parameters

In PHP, the function can accept the corresponding parameters, although these parameters are accepted outside the function of the value, but after exiting the function can not access these parameters, after the execution of the function, the value of the parameter disappears, and the function of the execution of a great relationship. The function argument is declared inside the parentheses following the function, and an example is created with this parameter, as shown in code 2.14.

Code 2.14 using Function parameters

function Echonum ($age, $class)

{

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

echo "Class:". $class;

}

Echonum (18, "Computer Technology and Science Department 08 Class 5 class");

?>

Execute the code, and the result is as follows:

Your age is: 18

Class: Computer Technology and Science Department 08 Class 5

Function parameters can also be referred to as local variables, which means that these parameters function only inside functions and cannot be accessed outside of the function, as well as when the function is executed at the end, the variable is also revoked.

3. Global variables

Global variables can be accessed from anywhere in the entire PHP program, but if you want to modify a global variable, you must explicitly declare it as a global variable in the function that modifies the variable, and it is simple to display the declaration of a global variable in a function simply by using the Global keyword declaration in the function. Here's an example of using a global variable that explains how to modify a global variable in a function, as shown in code 2.15.

Code 2.15 Modifying global variables in a function

function Addnum ()

{

Global $num;

$num = $num + $num;

Echo $num;

}

$num = 100;

Addnum ();

?>

Execute code 2.15, and the result is as follows:

200

If you do not add global before $num, the variable is considered a local variable, and the value shown on the page is 0; After adding global, you can modify the global variable, and another way to declare a global variable is to use PHP's $globals array. Using this array with the effect of using global, create an example of using the $globals array, as shown in code 2.16.

Code 2.16 using the $globals array

function Addnum ()

{

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

echo "This value is:". $GLOBALS [' num '];

}

$num = 100;

Addnum ();

?>

Execute code 2.16, and the result is as follows:

The value is: 200

When using global variables, it's important to note that because using global variables can be quite unexpected.

4. static variables

Static variables have a constant value between the two call functions, static variables are declared only in local function fields, and a static variable can be declared with the keyword static. Static variables do not lose value when the function exits, and when you call this function again, you can retain the value. The following creates an example of using a static variable, as shown in code 2.17.

Code 2.17 using static variables

function Keepnum ()

{

static $num = 0;

$num + +;

echo "Now the value of the static variable is:". $num;

echo "<br/>";

}

$num = 10;

The value of the echo "variable num is:". $num. "
";

Keepnum ();

Keepnum ();

?>

Execute code 2.17, and the result is as follows:

The value of the variable num is: 10

Now the value of the static variable is: 1

Now the value of the static variable is: 2

Known by code 2.17 execution, because the variable is static in the function, the preceding value is preserved when the function is executed.

Variables for 2.3.3 variables

To create a variable, you need to assign a value to it, you can also reference an assignment, and sometimes you need to use the variable's contents as the variable name, which means that the variable is stored in another variable. Variables that define variables only need to be preceded by a dollar sign in front of the variable. An example is created below, as shown in the following example code:

$name = "Yound";

$ $name = "Tang";

Echo $name. $ $name;

?>

Execute the end code, and the execution results are as follows:

Yound Tang

By the above example, a mutable variable gets the value of an ordinary variable as the variable name of the variable variable. Can be used as a variable variable, at this point, two 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.