Introduction to the basics of php variables

伊谢尔伦
Release: 2016-11-24 13:49:22
Original
946 people have browsed it

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.

$var = 'Bob';

$Var = 'Joe';

echo "$var, $Var"; // Output "Bob, Joe"

$4site = 'not yet'; // Illegal variable name; starts with a number

$_4site = 'not yet'; // Legal variable name; starts with underscore

$isite is = 'mansikka'; // Legal variable name; You can use Chinese

?>

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 = &#39;Bob&#39;;              // 将 &#39;Bob&#39; 赋给 $foo
$bar = &$foo;              // 通过 $bar 引用 $foo
$bar = "My name is $bar";  // 修改 $bar 变量
echo $bar;
echo $foo;                 // $foo 的值也被修改
?>
Copy after login

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();    // 非法
?>
Copy after login

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 &#39;false&#39; (See ternary operators for more on this syntax)
echo($unset_bool ? "true\n" : "false\n");
 
// String usage; outputs &#39;string(3) "abc"&#39;
$unset_str .= &#39;abc&#39;;
var_dump($unset_str);
 
// Integer usage; outputs &#39;int(25)&#39;
$unset_int += 25; // 0 + 25 => 25
var_dump($unset_int);
 
// Float/double usage; outputs &#39;float(1.25)&#39;
$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 = &#39;bar&#39;;
var_dump($unset_obj);
?>
Copy after login

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.


Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!