How do I use references & symbols? PHP & Symbolic usage examples

Source: Internet
Author: User
Tags date1
What is a php reference?

The reference in php means: Different names access the same variable content . PHP references (that is, the variables or functions, objects, etc. are preceded by the & symbol). Does it look like you're picking up orders, here's a little bit of explanation:

1. References to variables

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

Example one:

<?php$a= "", $b =& $a, echo $a;//output here: 2010echo $b;//output here: 2010$b= ", echo $a;//Here the value of $ A is changed to 2012 so output echo $b;// Output 2012?> here

Example two:

<?php$a = "date"; $b = & $a; echo $a; Dateecho $b; date$b = "Date1"; echo $a; Date1echo $b; Date1unset ($a); Echo $b; Date1?>

From the two examples above, it can be seen that the memory address of the $b to the $b, is not a simple assignment. So to $b

will also affect the $a

Another way is to add an alias to $a $b, if you delete the $a, just delete the name of the variable, and do not delete the contents of the variable, alias or can be used to display the contents of the variable. Relationship

2. Invocation of function

Example three:

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

How to Test here (1); Then it will go wrong.

The description parameter can only be a variable, and the constant does not have a pass address.

3. A reference to a function returns

Function reference return is used in the object, it is easy to understand the use of static variables to do an example

Example four:

<?phpfunction &test () {    static $b =0;//declares a static variable    $b = $b +1;    echo $b;    return $b;} This statement outputs the value of the $b 1$a=test (), $a =5; $a =test ();//This statement outputs a value of $b of 2 $a =&test ();//This statement outputs a value of $b 3$a=5; $a =test ();//This statement outputs $ The value of B is 6?>

Note that this function is output and has a return value.

$a = Test () ; just set the function test return value $b The is assigned to $a , which is a very common assignment, not a function reference to return. So $a no matter what you do, it doesn't affect .

$a = &test (); The function is to point the $b memory address to the $a memory address to the same place, will produce similar to the $b = & $a effect, if the $a value changed, it becomes 5, the value of $b is also affected. So in the execution $a = &test (),$a = 5, there is $b = 5, after the function processing output $b = 6;

4. references to Objects

Example five:

<?phpclass A{var $ABC = "abc";} $b =new A; $c = $b; echo $b->abc;//here output abcecho $c->abc;//output abc$b->abc= "DEF"; Echo $c->abc;//here output def?>

The above code is the effect of running in PHP5 , and the copy of the object in PHP5 is implemented by reference.

The above $b =new A; $c = $b; In fact, it is equivalent to $b =new A; $c =& $b;

The default in PHP5 is to invoke the object by reference, but sometimes you might want to make a copy of the object, and you want the original object to change without affecting the copy. for this purpose,PHP defines a special method called clone.

5. Role of references

If the program is larger, referencing the same object is more variable , and you want to use the object to manually clear it , it is recommended to use the "&" method , and then use $var =null the way to clear. At other times, it's the default way to use php5 .

In addition , in the php5 for large arrays of delivery , it is recommended to use "&" mode, after all, saving memory space to use.

6. dereference

When you unset a reference, you just break the binding between the variable name and the variable content. This does not mean that the contents of the variable are destroyed.

For example:

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

Not unset $b, just $a.

You can see the reference to the variable.

7. Global references

When you declare a variable with the global $var , you actually establish a reference to the global variable.

It is equivalent to the following code:

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

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

8. $this

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

Another explanation

The point (like pointer) function of the address in PHP is not implemented by the user itself, it is implemented by the Zend core, and the reference inPHP is " copy-on-write " the principle is that unless a write operation occurs, it is copied, other operations, a variable that points to the same address, or the object is not copied.

If you have the following code:

$a = "ABC";
$b = $a;

At this point,$a and $b all point to the same memory address, not $a memory that is different from the $b

If you add the code above, the following code

$a = "EFG";

Here's a "write" operation.

Since $ A and $b point to the memory of the data to be re-written once, at this time Zend core will automatically determine, automatically for $b production of a $ A copy of the data, re-request a piece of memory for storage.

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.