Basic syntax for PHP

Source: Internet
Author: User

1. variable variable

Variable names for a variable can be set and used dynamically. An ordinary variable is set by a declaration, and a mutable variable gets the value of a normal variable as the variable name of the variable variable, as follows:

<?php

$hi = "Hello";
$ $hi = "word";
echo "$hi $hello <br>"; Output "Hello World"
echo "$hi ${$hi}"; Output "Hello World"

In the example above, "HI" uses two dollar sign ($) and can be used as a variable variable. At this point two variables are defined, $hi content is "Hello", and the content of $hello is "world". The above two outputs specify that the "Hello World" will be output. That is, $ $hi and $hello are equivalent.

2. Assigning a reference to a variable

Variables are always assigned values. That is, 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 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.

PHP provides another way to assign a value to a variable: a reference assignment. This means that the new variable simply references (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, so this replication is faster. However, it is only possible to notice the increase in speed when a dense loop or a large array or object is assigned a value. Use reference assignment to simply add a "&" resurrection to the variable that will be assigned (the source variable). For example, the following code fragment shows:

<?php

$foo = "Bob";
$bar = & $foo;

$bar = "My name is Tom"; Change the value of a variable $bar
echo $bar. "    <br> "; Variable $bar value is changed, output "My name is Tom"
echo $foo. "    <br> "; The value of the variable $foo is also changed to output "My name is Tom"

$foo = "Your name is Bob"; Change the value of a variable $foo
echo $bar. "       <br> "; The value of the variable $bar is also changed, output "Your name is Bob"
echo $foo. "       <br> "; The value of the variable $foo is changed, and the output "Your name is Bob"

In the above code, we do not assign the value of the variable $foo to the variable $bar, but instead assign the $foo reference to $bar, at which point the $bar equals the alias of the $foo. As long as any one of these changes, it will affect the other variable. One important point to note is that only variables with a name can reference the assignment. As shown below:

<?php

$foo = 25;

$bar = & $foo; This is a valid reference assignment

$bar = & (24 *47);//This reference assignment is not valid and the expression cannot be assigned as a reference

function Test () {

return 25;

}

$bar = &test (); This reference assignment is not valid and is also a variable with no Name

In addition, PHP references are not like address pointers in the C language. For example, in the expression $bar = & $foo, it does not cause $bar and $foo to be in memory, but to correlate the respective values together. Based on this, using unset () does not cause all reference variables to disappear.

<?php

$foo = 25;

$bar = & $foo;

Unset ($bar);

Echo $foo; Value of 25

After unset () is executed, the variables $bar and $foo are only associated with each other, and the $foo is not lost because of the $bar release.

Basic syntax for PHP

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.