Home > Backend Development > PHP Tutorial > PHP Section 3 Introduction to Variables_PHP Tutorial

PHP Section 3 Introduction to Variables_PHP Tutorial

WBOY
Release: 2016-07-21 15:19:35
Original
832 people have browsed it

Variable names follow the same rules as other tags in PHP. A valid variable name begins with a letter or an 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 that cannot be assigned a value.

Variables are always assigned by value by default. That is, when the value of an expression is assigned 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 will not affect the other variable when the value of one variable is assigned to another variable. See the chapter Expressions for this type of assignment operation.
PHP also provides another way to assign values ​​to variables: reference assignment. This means that the new variable simply references (in other words, "aliases" or "points to") the original variable. Changing the new variable will affect the original variable and vice versa.

To use assignment by reference, simply add an & symbol in front of the variable to be assigned (the source variable).

One important thing must be pointed out, that is, only variables with names can be assigned by reference.

Copy code The code is as follows:

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

$bar = &test(); // Illegal
?>

Although there is no need to initialize variables in PHP, initializing variables is a Good habits. Uninitialized variables have a default value for their type - FALSE for Boolean variables, zero for integer and floating-point variables, the empty string for string variables, or the empty array for array variables. .

Relying on default values ​​for uninitialized variables can be problematic in certain situations, such as when including one file into another with the same variable name. Also turning register_globals on is a major security risk. Using an uninitialized variable will issue an E_NOTICE error, but appending elements to an uninitialized array will not. The isset() language construct can be used to detect whether a variable has been initialized.

Predefined variables
PHP provides a large number of predefined variables. Detailed documentation is not available as many variables depend on the version and settings of the running server, among other factors. Some predefined variables do not take effect when PHP is run from the command line.

Starting with PHP 4.1.0, PHP provides an additional set of predetermined array variables that contain data from the web server (if available), the runtime environment, and user input. These arrays are very special in that they automatically take effect globally, i.e. automatically in any scope. Therefore they are often called autoglobals or superglobals. (There is no mechanism for user-defined superglobal variables in PHP.) Superglobal variables are listed below; but for their contents and further discussion of PHP's predefined variables and their nature, see Predefined Variables. Furthermore, you will also notice that the old predefined arrays ($HTTP_*_VARS) are still present. As of PHP 5.0.0, long-type PHP predefined variable arrays can be disabled with the register_long_arrays setting option.

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

PHP’s global variables are a little different from C language. In C language, global variables automatically take effect in functions unless overridden by local variables. Local variables in PHP will not be overwritten by global variables. If used, they will be the default initial values ​​of the variables. This may cause some problems, someone may accidentally change a global variable. Global variables in PHP must be declared global when used in functions.

Using static variables
Another important feature of variable scope is static variables. Static variables only exist in the local function scope, but their values ​​are not lost when program execution leaves this scope.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325295.htmlTechArticleVariable names follow the same rules as other tags in PHP. A valid variable name begins with a letter or an underscore, followed by any number of letters, numbers, or underscores. ...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template