Reference & Usage Analysis in PHP "variable reference, function reference, object reference" _php tips

Source: Internet
Author: User
Tags php programming php regular expression zend

The example of this article analyzes the usage of reference & in PHP. Share to everyone for your reference, specific as follows:

PHP references (that is, precede variables or functions, objects, etc. & symbols)//The most important thing is to delete the referenced variable, only the referenced variable cannot be accessed, but the content is not destroyed

References in PHP mean that different names access the same variable content.

References to variables

PHP references allow you to use two variables to point to the same content

<?php
$a = "ABC"; 
$b =& $a; 
echo $a//Here output: ABC 
echo $b//Here output: ABC 
$b = "EFG"; 
echo $a//Here the value of $a becomes EFG so output EFG echo $b;//Here output EFG 
?>

Call to an address call for a function I don't have to say more. The following directly gives the code

<?php
function Test (& $a) { 
  $a = $a +100; 
} 
$b =1; 
echo $b//Output 1 Test ($b);  Here $b passed to the function is actually the $b variable content of the memory address, by changing the value of $a in the function can change the value of the $b echo "<br>"; echo $b;/output
?>

Note that the test (1) here will make an error because the reference passed by PHP cannot be a constant (you can see the error hint).

Function reference returns first look at the code

<?php
function &test () { 
  static $b =0;//declares a static variable 
  $b = $b +1; 
  echo $b; 
  return $b;
$a =test ();//This statement outputs $b value of 1 
$a =5; $a =test ();//This statement outputs a value of $b of 2
$a =&test ();//This statement outputs $b value of 3 
$a =5 $a =test ();//This statement will output $b value of 6
?>

The following explains: $a=test () in this way; The result is not the return of a reference to a function, which is no different from a normal function call. As for the reason: This is the PHP stipulation that PHP rules through $a=&test (); The way to get is the function of the reference to return as to what is the reference to return it (the PHP manual says: Reference return is used when you want to use a function to find the reference should be bound to which variable above.) This shit is killing me for half a day.

The above example to explain is $a =test () way to call a function, but the value of the function to $a only, and $a do any changes will not affect the $b in the function, and through the $a=&test () way to call the function, his role is to return $b $ The memory address of the B variable and the memory address of the $a variable point to the same place that produces the equivalent effect ($a =&b;) So changing the value of the $a also changes the value of the $b so that in the execution of the $a =&test (); $a = 5; Later, the value of $b changed to 5.

Here is to let you understand the function of reference to return to use static variables, in fact, the function of reference return is more used in the object

References to Objects

<?php
class a{
  var $abc = "abc";
} 
$b =new A; 
$c = $b; 
echo $b->abc;//Here output 
the ABC echo $c->abc;//here the ABC $b->abc= "DEF"; 
echo $c->abc;//here output def
?>

The above code is in the PHP5 effect in the PHP5 the object's replication is achieved by reference. The above $b=new A; $c = $b; is actually equivalent to $b=new A; $c =& $b; The default in PHP5 is to invoke an object by reference, but sometimes you might want to create a copy of an object and expect the original object to change without affecting the copy. For this purpose, PHP defines a special method called __clone.

The role of a reference

If the program is larger, the number of references to the same object is more, and you want to use the object to remove it manually, personal advice to use the "&" mode, and then clear the $var=null. At other times, use the PHP5 default method. In addition, in PHP5 for large array of transmission, it is recommended to use the "&" mode, after all, save memory space use.

Dereference when you unset a reference, just disconnect the variable name and the contents of the variable binding. This does not mean that the variable content has been destroyed. For example:

<?php $a = 1; $b =& $a; unset ($a);?>

Not unset $b, just $a. function Quotetest () {global $var;///equivalent to $var = & $GLOBALS [' var ']; unset ($var);//Delete just deletes the reference, and the referenced content still exists, Ditto this does not mean that the variable contents are destroyed} $var =1;quotetest (); Echo $var; Result 1

Not unset $b, just $a.

function Quotetest () {global $var;//equals $var = & $GLOBALS [' var ']; $var = 5;//Because they all point to the same memory content} $var =1;quotetest (); Echo $var; Result 5

' & ' That's the reference

The global reference actually establishes a reference to a global variable when declaring a variable with the global $var. In other words, it is the same as doing this:

<?php $var =& $GLOBALS ["var"];?>

This means, for example, that unset $var does not unset global variables.

$this in the method of an object, $this is always a reference to the object that called it.

Here's a little episode. The pointer to address (like pointers) in PHP is not implemented by the user itself, is implemented by the Zend Core, PHP refers to the use of "write-time copy" principle, unless a write operation, point to the same address variables or objects are not copied.

In layman's terms.

1. If there is the following code [PHP] $a = "ABC"; $b = $a; [/php] In fact at this point $a and $b are pointing to the same memory address and not $a and $b occupy different memory

2. If the above code is based on the following code [PHP] $a = "EFG"; [/php] because the $a and $b point to the memory of the data to be written again, the Zend core will automatically judge automatically for $b production of a $a copy of the data, reapply a piece of memory for storage

More about PHP Interested readers can view the site topics: "PHP commonly used functions and skills summary", "PHP object-oriented Program Design Introductory Course", "PHP Mathematical Arithmetic Skills summary", "PHP Array" operation Skills Encyclopedia, "PHP Data structure and algorithm tutorial", " PHP Programming algorithm Summary, "PHP Regular Expression Usage summary" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Related Article

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.