Home > Backend Development > PHP Tutorial > Understanding and using PHP's & quote character_PHP Tutorial

Understanding and using PHP's & quote character_PHP Tutorial

WBOY
Release: 2016-07-13 10:33:29
Original
833 people have browsed it

Regarding the role of PHP references (that is, adding the ampersand in front of variables, functions, objects, etc.), let’s first look at the following program.

    
<?php
$a = 100; //声明变量a
$b = &$a; //声明变量b,引用自变量a
echo "$a <br />";  
echo "$b <br />";
$a++; //变量a自增1
echo "$a <br />";
echo "$b <br />";//查看变量b,也增加了1,说明使用的是同一存储单元
?>
Copy after login

Program execution result:

100 
100 
101 
101
Copy after login

Many people misunderstand that references in PHP are the same as pointers in C. In fact, they are not, and they are very different. Except for the pointers in C language that do not need to be explicitly declared during the array transfer process, other points need to be defined using *. However, the pointer to address (similar to a pointer) function in PHP is not implemented by the user himself, but is implemented by the Zend core. Yes, the reference in PHP adopts the principle of "copy-on-write", that is, unless a write operation occurs, variables or objects pointing to the same address will not be copied.

php defaults to passing by value:

 
<?php    
$a = 20;
$b = $a;
$a = $a + 10; 
echo $a.' and '.$b; 
?>
Copy after login

Program execution result:

30 and 20
Copy after login

If you want to pass it as an address, you need to add &, that is:

  
<?php
$a = 20;
$b = &$a; 
$a = $a + 10; 
echo $a.' and '.$b; 
?>
Copy after login

Program execution result:

30 and 30
Copy after login

In other words, & passes the address of $a to $b. In this case, the two variables now share a memory storage area, which means that their values ​​are the same.

The same syntax can be used in functions, which return references, and in the new operator:

<?php
$bar =& new fooclass();
$foo =& find_var($bar);
?>
Copy after login

The second thing a reference does is pass a variable by reference. This is accomplished by creating a local variable within the function that references the same content in the calling scope. To put it simply, the parameter of a function is a reference to a local variable. Here’s another example:

<?php
function foo(&$val1, $val2) {
	$val1 += 1;
	$val2 += 1;
}
$a=5;
$b=10;
foo($a,$b);
echo $a;
echo $b;
?>
Copy after login

To run this code, you pass two parameters to the function, one is a reference to the content of $a, and the other is the value of $b. After executing this function, it is found that the content of $a has changed, while the content of $b is No changes.

The third usage of PHP references is reference return. This usage is a bit difficult to understand. Reference return is used when you want to use a function to find which variable the reference should be bound to. When returning a reference, use this syntax: To put it simply, it is still the return of the reference function. But unlike parameter passing, the & symbol must be used in both function definition and function reference. Here’s an example:

<?php
function &find_var ($param)
{
    /* ...code... */
    return $found_var;
}
$foo =& find_var ($bar);
$foo->x = 2;
?>
Copy after login

In this example, the assignment to $foo is the return reference of the function find_var, so when assigning a value to $foo->x, the return reference of find_var is assigned instead of a simple assignment.

The last usage of PHP references is reference positioning. There are two main applications: one is global reference. When declaring a variable with global $var, a reference to the global variable is actually established. That is, it is the same as $var =& $GLOBALS["var"];. Another one is the usage of $this. In a method of an object, $this is always a reference to the object that calls it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752461.htmlTechArticleAbout the role of PHP references (that is, adding symbols in front of variables, functions, objects, etc.), let’s first look at Below is the program. ?php$a = 100; //Declare variable a$b = //Declare variable b, reference...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template