Home > Article > Backend Development > Introduction to the basics of php variables
Variables in PHP are represented by a dollar sign followed by the variable name. Variable names are case-sensitive.
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 normal regular expressions, it will be expressed as: '[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*'.
Note: The letters mentioned here are a-z, A-Z, and ASCII characters from 127 to 255 (0x7f-0xff).
Note: $this is a special variable, it cannot be assigned a value.
1d323463b6a1b02c3fe0fba104c8ac0e
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 while the value of one variable is assigned to another variable will not affect the other variable.
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 reference assignment, simply add an & sign in front of the variable to be assigned (the source variable). For example, the following code snippet will print "My name is Bob" twice:
<?php $foo = 'Bob'; // 将 'Bob' 赋给 $foo $bar = &$foo; // 通过 $bar 引用 $foo $bar = "My name is $bar"; // 修改 $bar 变量 echo $bar; echo $foo; // $foo 的值也被修改 ?>
One important thing to point out is that only named variables can be assigned by reference.
<?php $foo = 25; $bar = &$foo; // 合法的赋值 $bar = &(24 * 7); // 非法; 引用没有名字的表达式 function test() { return 25; } $bar = &test(); // 非法 ?>
Although it is not necessary to initialize variables in PHP, it is a good habit to initialize variables. Uninitialized variables have a default value for their type - Boolean variables have a default value of FALSE, integer and floating point variables have a default value of zero, string variables (such as those used in echo) have a default value of the empty string and arrays The default value of a variable is an empty array.
Example #1 Default value of uninitialized variable
<?php // Unset AND unreferenced (no use context) variable; outputs NULL var_dump($unset_var); // Boolean usage; outputs 'false' (See ternary operators for more on this syntax) echo($unset_bool ? "true\n" : "false\n"); // String usage; outputs 'string(3) "abc"' $unset_str .= 'abc'; var_dump($unset_str); // Integer usage; outputs 'int(25)' $unset_int += 25; // 0 + 25 => 25 var_dump($unset_int); // Float/double usage; outputs 'float(1.25)' $unset_float += 1.25; var_dump($unset_float); // Array usage; outputs array(1) { [3]=> string(3) "def" } $unset_arr[3] = "def"; // array() + array(3 => "def") => array(3 => "def") var_dump($unset_arr); // Object usage; creates new stdClass object (see http://www.php.net/manual/en/reserved.classes.php) // Outputs: object(stdClass)#1 (1) { ["foo"]=> string(3) "bar" } $unset_obj->foo = 'bar'; var_dump($unset_obj); ?>
Relying on the default value of uninitialized variable 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.