PHP Variables Primer Tutorial (1) Basics

Source: Internet
Author: User

Basis

A dollar sign in PHP followed by a variable name, which represents a variable. The name of the variable is case-sensitive.

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: The letters are A-Z,A-Z,ASCII characters from 127 to 255 (0X7F-0XFF).

<?php$var = "Bob"; $Var = "Joe"; echo "$var, $Var";      Outputs "Bob, Joe" $4site = ' not yet ';    Invalid Starts with a number$_4site = ' not yet ';    Valid Starts with an underscore$t?yte = ' mansikka ';    valid;? Is (Extended) ASCII 228.? >

In PHP 3, variables are always assigned values. 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, 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 4 provides another way to assign a value to a variable: pass an address 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. This also means that the copy operation is not performed, and thus the assignment is faster. However, any speed-up operation can only be noticed in tight loops or large arrays or objects.

Using a pass-through address assignment, simply append A (&) symbol to the variable that will be assigned (the source variable). For example, the following code fragment two times output ' My name is Bob ':

<?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 pass address assignments.

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

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

Warning

PHP 4.2.0 and subsequent versions, the default value of PHP directive register_globals is off. This is a major change in PHP. Letting the register_globals value off will affect the validity of the set of predefined variables at global scope. For example, in order to get the value of the Document_root, you would have to use $_server[' document_root ' instead of the $DOCUMENT _root, as in the case of the $_get[' ID ') instead of the $id from the URL http://www.ex Get the ID value in ample.com/test.php?id=3, or use $_env[' home ' instead of $HOME get the value of the environment variable HOME.

For more information, read the configuration item register_globals, a chapter on security using the register globals, and the release notices for PHP 4.1.0 and 4.2.0.

Use the available PHP pre-defined variables, such as super global arrays, first.

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. For this reason, they are often known for being "autoglobals" or "superglobals". (There is no mechanism in PHP that allows users to customize super-global variables.) The super global variables are listed below; but 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. Since PHP 5.0.0, lengthy PHP pre-defined variables can be masked by setting register_long_arrays.

mutable variables: Super Global variables cannot be used as mutable variables.

If the variables in some variables_order are not set, their corresponding PHP predefined arrays are also empty.

PHP variable Getting Started tutorial (1) Basics

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.