Php assignment and reference assignment instructions

Source: Internet
Author: User
In php, assign values to variables and assign values to 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, the instance code is as follows:

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. the instance code is as follows:

  1. $ Number = 15;
  2. $ Age = 20;
  3. $ Sum = 12 + "15"; // $ sum = 27

2. there are two methods to create an array: variable assignment and function call. here we will first talk about the former. the method to assign values to an array variable is very simple. the instance code is as follows:

  1. $ Lang [] = "php ";
  2. $ Lang [] = "html ";
  3. $ Lang [] = "css ";
  4. Echo "$ lang [0]
    ";
  5. Echo "$ lang [1]
    ";
  6. Echo "$ lang [2]
    ";
  7. >

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, modify any of the variables, all other variables are 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:

  1. $ Val1 = "hello ";
  2. $ Val2 = & $ val1;
  3. $ Val2 = "goodby ";
  4. Echo '$ val1 is'. $ val1 ."
  5. ";
  6. Echo '$ val2 is'. $ val2 ."
  7. ";
  8. ?>
  9. $ Val1 is goodby
  10. $ Val2 is goodby

The code for the foreach reference assignment is as follows:

  1. $ A = array (
  2. 'A' => 'A ',
  3. 'B' => 'BB ',
  4. 'C' => 'CC ',
  5. );
  6. Foreach ($ a as & $ v ){
  7. ;
  8. }
  9. Print_r ($ );
  10.  
  11. Foreach ($ a as $ v ){
  12. }
  13. 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.

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.