Pretty rhythm Dear My future PHP third section variable introduction

Source: Internet
Author: User
The variable name follows the same rules as other tags in PHP. A valid variable name begins with a letter or underscore, 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, it cannot be assigned a value.
Variables are always assigned values by default. That is, when you assign the value of an expression to a variable, the value of the entire original expression is assigned to the target variable. This means that, for example, when the value of one variable is assigned to another variable, changing the value of one of the variables will not affect the other variable. For assignment operations of this type, see the expression chapter.
PHP also provides another way to assign a value to a variable: reference assignment. This means that the new variable is a simple reference (in other words, "becomes its alias" or "points to") the original variable. Changing the new variable will affect the original variable and vice versa.
Using a reference assignment, simply add a & symbol to the variable that will be assigned (the source variable).
It is important to point out that only a variable with a name can reference the assignment.

Copy the Code code as follows:


$foo = 25;
$bar = & $foo; Legal assignment
$bar = & (24 * 7); Illegal Referencing an expression without a name
function test ()
{
return 25;
}
$bar = &test (); Illegal
?>


Although it is not necessary to initialize variables in PHP, it is a good practice to initialize variables. Uninitialized variables have default values of their type-the default value for a variable of type Boolean is FALSE, the default value for shaping and floating-point variables is zero, the default value for a string variable is an empty string, or the default value of an array variable is an empty array.
Default values that depend on uninitialized variables can be problematic in some cases, such as when one file is included in the other and the same variable name is encountered. In addition, opening the register_globals is a major security risk. Using an uninitialized variable emits a e_notice error, but not when attaching a unit to an uninitialized array. The isset () language structure can be used to detect whether a variable has been initialized.
Pre-defined variables
PHP provides a large number of predefined variables. Because many variables depend on the version and settings of the server that is running, and other factors, no detailed documentation is provided. Some of the predefined variables do not take effect when PHP is run as a command line.
Starting with PHP 4.1.0, PHP provides an additional set of predefined arrays that contain data from the Web server (if available), the runtime environment, and user input. These arrays are very special and they take effect automatically in the global scope, for example, automatically in any scope. This is often referred to as an automatic global variable (autoglobals) or a Hyper global variable (superglobals). (There is no mechanism for users to customize hyper-global variables in PHP.) The hyper-global variables are listed below, but in order to get their content and further discussion about the predefined variables of PHP and their nature, see predefined variables. Also, you will notice that the old pre-defined array ($HTTP _*_vars) still exists. From PHP 5.0.0, use the Register_long_arrays setting option to disable the long type of PHP predefined variable array.
Variable Range
The scope of a variable is the context range it defines (that is, its effective scope). Most PHP variables have a single range. This separate scope span also contains the files introduced by include and require.
PHP has a slightly different global variable from the C language, and in C, global variables are automatically applied in functions unless overridden by local variables. Local variables in PHP are not overwritten by global variables, and if used, they are the default initial values for variables. This can cause some problems, and some people may accidentally change a global variable. In PHP, global variables must be declared as globals when used in functions.
Using static variables
Another important feature of the variable range is the static variable (variable). A static variable exists only in the local function domain, but its value is not lost when the program executes away from the scope.

The above describes the pretty rhythm dear My future PHP Third section variable introduction, including pretty rhythm dear the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.