PHP variable description (1)

Source: Internet
Author: User

Basic

In PHP, a dollar sign is followed by a variable name, that is, a variable. Variable names are case sensitive.

Variable names 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: The letter is a-z, A-Z, ASCII characters from 127 to 255 (0x7f-0xff ).

<? Php $ var = "Bob"; $ Var = "Joe"; echo "$ var, $ Var"; // outputs "Bob, joe "$4 site = 'not yun'; // invalid; starts with a number $ _ 4 site = 'not yun'; // valid; starts with an underscore $ t? Yte = 'mansikka '; // valid; 'region is (Extended) ASCII 228.?>

In PHP 3, values are always assigned to variables. That is to say, 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, 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 4 provides another way to assign values to variables: assign values to input addresses. 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. This also means that no copy operation is executed. Therefore, this assignment operation is faster. However, any speed-up operation can only be noticed in close loops, large arrays, or objects.

Assign values using the transfer address. simply append a (&) symbol to the variable to be assigned (source variable ). For example, the following code snippet outputs 'My name is bob' twice ':

<?php$foo = 'Bob';              // Assign the value 'Bob' to $foo$bar = &$foo;              // Reference $foo via $bar.$bar = "My name is $bar";  // Alter $bar...echo $bar;echo $foo;                // $foo is altered too.?>  

It is important to note that only named variables can assign values to addresses.

<?php$foo = 25;$bar = &$foo;      // This is a valid assignment.$bar = &(24 * 7);  // Invalid; references an unnamed expression.function test(){   return 25;}$bar = &test();    // Invalid.?> 

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. For a detailed list of these variables, see the "Reserved predefined variables" chapter.

Warning
In PHP 4.2.0 and later versions, the default value of the PHP Command register_globals is off. This is a major change in PHP. Setting the value of register_globals to off affects the validity of the predefined variable set within the global range. For example, to obtain the DOCUMENT_ROOT value, you must replace $ _ SERVER ['document _ root'] with $ DOCUMENT_ROOT, as shown in the following figure, use $ _ GET ['id'] to replace $ id from URL

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.