PHP Assignment and reference Assignment usage Instructions _php Tutorial

Source: Internet
Author: User
Assignment in PHP is assigned to a variable assignment and reference assignment, below I will give you some of their basic usage and differences.

1, Value assignment: Copies the value of an assignment expression to a variable. Example:

The code is as follows Copy Code

$number = 15;
$age = 20;
$sum "=12+";//$sum =27


2. Create the array has variable assignment and call function two methods, here first of the former.

It is very simple to assign a value to an array variable by using the variable assignment method.

Instance:

The code is as follows Copy Code

$lang []= "PHP";
$lang []= "HTML";
$lang []= "CSS";
echo "$lang [0]
";
echo "$lang [1]
";
echo "$lang [2]
";
?>

Array contents generated by three assignment statements:

0=>php

1=>html

2=>css

3. Reference assignment: The variable created is the same as the content referenced by another variable. So if more than one variable references the same content, modifying any of the variables will be reflected on the remaining variables. Add a & symbol after the equals sign ($val 2=& $val 1) To complete the reference assignment or put the & symbol in front of the referenced variable ($val 2= & $val 1):

The code is as follows Copy Code

$val 1= "Hello";
$val 2=& $val 1;
$val 2= "Goodby";
Echo ' $val 1 is '. $val 1. "
";
Echo ' $val 2 is '. $val 2. "
";
?>

$val 1 is Goodby
$val 2 is Goodby


Problems with the assignment of a foreach reference

Code:

The code is as follows Copy Code

$a = Array (
' A ' = ' AA ',
' B ' = ' BB ',
' C ' = ' cc ',
);
foreach ($a as & $v) {
;
}
Print_r ($a);


foreach ($a as $v) {

}

Print_r ($a);


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

Think carefully, in fact, is a simple reference problem, the first foreach is finished, $v is actually $a[' C ') reference, the loop, each time the assignment to $v will change the value of $a[' C '], and the last assignment to $v is $v= $a [' C '], $a [' C '] in the last assignment was $a [' B '], so this will happen

http://www.bkjia.com/PHPjc/631537.html www.bkjia.com true http://www.bkjia.com/PHPjc/631537.html techarticle Assignment in PHP is assigned to a variable assignment and reference assignment, below I will give you some of their basic usage and differences. 1, Value assignment: Copies the value of an assignment expression to a variable. ...

  • 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.