PHP section 3 variables

Source: Internet
Author: User
Variables in PHP are represented by a dollar sign followed by the variable name. Variable names are case-sensitive and follow the same rules as other labels in PHP. A valid variable name must start with a letter or underline followed by any number of letters, numbers, or underscores. According to the normal regular expression, it will be expressed as: '[a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] *'.

Note: $ this is a special variable and cannot be assigned a value.

By default, values are always assigned. That is to say, when the value of an expression is assigned to a variable, the value of the original expression is assigned to the target variable. This means that, for example, changing the value of one variable when the value of a variable is assigned to another variable does not affect another variable. For this type of assignment operation, see the expression chapter.
PHP also provides another way to assign values to variables: assign values to references. This means that the new variable simply references the original variable (in other words, "become its alias" or "point. Changing new variables will affect the original variables, and vice versa.

Assign values by reference. simply add an & symbol to the variable to be assigned (source variable ).

It is important to note that only variables with names can reference assignment.
The code is as follows:
$ Foo = 25;
$ Bar = & $ foo; // valid value assignment
$ Bar = & (24*7); // invalid; reference an expression without a name
Function test ()
{
Return 25;
}

$ Bar = & test (); // invalid
?>

Although you do not need to initialize variables in PHP, it is a good habit to initialize variables. Uninitialized variables have default values of their types-Boolean variables have default values of FALSE and integer and floating-point variables have default values of zero, the default value of a string variable is an empty string or an array variable. the default value is an empty array.

Dependent on the default value of uninitialized variables may cause problems in some cases. for example, when a file is included in another, the same variable name may occur. In addition, opening register_globals is a major security risk. An E_NOTICE error occurs when uninitialized variables are used, but not when a cell is attached to an uninitialized array. The isset () language structure can be used to check whether a variable has been initialized.

Predefined variables
PHP provides a large number of predefined variables. Because many variables depend on the version and settings of the running server, and other factors, there is no detailed description document. Some predefined variables do not take effect when PHP is run as a command line.

Starting from PHP 4.1.0, PHP provides an additional set of pre-defined arrays that contain data from the web server (if available), runtime environment, and user input. These arrays are very special and they take effect globally, for example, automatically within any range. Therefore, it is usually called autoglobals or superglobals ). (PHP does not have a mechanism for customizing global variables .) Hyperglobal variables are listed below. For more information about them, PHP predefined variables, and their nature, see predefine variables. In addition, you will also notice that the old pre-defined array ($ HTTP _ * _ VARS) still exists. From PHP 5.0.0, you can use register_long_arrays to disable long PHP pre-defined variable arrays.

Variable range
The scope of a variable is the context scope defined by it (that is, its effective range ). Most PHP variables have only one separate range. This separate range span also contains the files introduced by include and require.

The global variables in PHP are a little different from those in C language. in C language, global variables automatically take effect in functions unless they are overwritten by local variables. The local variables in PHP are not overwritten by global variables. if used, they are the default initial values of the variables. This may cause some problems. some may accidentally change a global variable. Global variables in PHP must be declared as global when used in functions.

Use static variables
Another important feature of variable range is static variable ). Static variables only exist in local function domains, but their values are not lost when the program runs out of this scope.

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.