This is the fourth day of my participation in Gwen Challenge

In recent days, I have read a lot about Reference and found that the core of Reference is very simple, but it may make mistakes if you are not careful. I will summarize this, sort out some rules and provide some guidance for everyone to avoid the pit.

I divide references into three reference scenarios: non-array variable references, array references, and function references. Each scenario will unfold according to the application operations of assign, pass, cancel, and so on.

The core logic of PHP references &

What is the purpose of a reference: assign, pass, return.

In the PHP context, referencing means using different variable names to access the same variable content. PHP variable names are aliases. A variable value can have multiple aliases, just like a person who has a big name, a nickname, or a pen name. This is not the same as a pointer in C.

Here’s an example:

$a=1; $b=2; Imagine this is the computer’s memory:

Pointer to the value PHP variable name
1 1 $a
2 2 $b

$c=&a

Pointer to the value The variable name
1 1 $a, $c
2 2 $b

$c=3; $c=3;

Pointer to the value The variable name
1 3 $a, $c
2 2 $b
$a has also changed

$a=&$b; $a=&$b;

Pointer to the value The variable name
1 3 $c
2 2 $b, $a
$c = $a; $c = $a

$e=&$a; $e=&$a;

Pointer to the value The variable name
1 3 $c
2 2 $b, $a, $e
It also points to 2

If $b = $b, unset($b);

Pointer to the value The variable name
1 3 $c
2 2 $a $e
$a = 2; $a = 2; $a = 2

Ok, so let’s summarize the core logic of referencing:

  1. A PHP reference is another alias for the same variable;
  2. There are no multiple references because they all point to the same variable value;
  3. The relationship of references is multidirectional, not unidirectional. If you change the value of a variable, other references to that variable change as well.
  4. When a reference variable is deleted, the association between the name of the variable and the value of the variable is removed, and other references are not affected.
  5. To assign a new reference to a variable, the previous reference relationship is deleted, and the operation on the variable no longer affects the previous reference value.

The above five core logic is not difficult to understand, but in practical application, there are many pits, such as the following:

$a = 1; $b =& $a; $c = $b; $c = 7; var_dump($a); It's int(1), why not 7? $c=$b; $c=$b; If $b=$c, the value of $a will be 7, because $b is 7. Note that reference associations must be passed by &. Assignments without & are not passed by reference, even if the variable has a reference to another variable.Copy the code

The first section is over, students, class dismissed!

Thank you for reading, if there is inaccurate and error, please leave a message to correct, I will timely correction, thank you!

Welcome you who love technology to communicate with me and learn together!

Summary is not easy, please do not reprint without permission, otherwise don’t blame old uncle you are welcome.

Reference: www.php.net official documentation