Php assignment and reference assignment instructions

Source: Internet
Author: User
In php, values are assigned to variables and references. next I will introduce some of their basic usage and differences. 1. value assignment: Copy the value of the value assignment expression to the variable. Example: the code is as follows: Copy the code...

In php, values are assigned to variables and references. next I will introduce some of their basic usage and differences.

1. value assignment: Copy the value of the value assignment expression to the variable. Example:

The code is as follows:

$ Number = 15;
$ Age = 20;
$ Sum = 12 + "15"; // $ sum = 27


2. there are two ways to create an array: variable assignment and function call. here we will first talk about the former.

You can assign values to an array variable directly.

Instance:

The code is as follows:

$ Lang [] = "php ";
$ Lang [] = "html ";
$ Lang [] = "css ";
Echo "$ lang [0]
";
Echo "$ lang [1]
";
Echo "$ lang [2]
";
?>

Array content generated by the three assignment statements:

0 => php

1 => html

2 => css

3. reference value assignment: the created variable is the same as that referenced by another variable. Therefore, if multiple variables reference the same content and modify any of the variables, the other variables will be reflected. Add an & symbol ($ val2 = & $ val1) after the equal sign to complete the reference assignment or put the & symbol before the referenced variable ($ val2 = & $ val1 ):

The code is as follows:

$ Val1 = "hello ";
$ Val2 = & $ val1;
$ Val2 = "goodby ";
Echo '$ val1 is'. $ val1 ."
";
Echo '$ val2 is'. $ val2 ."
";
?>

$ Val1 is goodby
$ Val2 is goodby


Foreach reference assignment

Code:

The code is as follows:

$ A = array (
'A' => 'A ',
'B' => 'BB ',
'C' => 'CC ',
);
Foreach ($ a as & $ v ){
;
}
Print_r ($ );


Foreach ($ a as $ v ){

}

Print_r ($ );


----------------------

If you think about it, it is actually a simple reference. after foreach is completed for the first time, $ v is actually a reference of $ a ['c']. during The Loop, each value assigned to $ v changes the value of $ a ['c']. the last value assigned to $ v is $ v = $ a ['c']. $ a ['c'] is assigned $ a ['B'] for the last time.



Tutorial link:

Reprint at will ~ However, please keep the tutorial address★

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.