Reference symbols and usage of php

Source: Internet
Author: User
Tags zend
The code is as follows: Copy code

<? Php
$ A = 'CC ';
$ B = $ a; // $ a and $ B point to the same memory address
$ A = 'DD ';
Echo $ B;
/*
The address pointing (similar to pointer) function in php is not implemented by the user,
It is implemented by the Zend core. The reference in php adopts the principle of "copy at Write Time,
Unless a write operation occurs, the variables or objects pointing to the same address will not be copied.
When $ a = 'DD ';
Because the memory data pointed to by $ a and $ B needs to be re-written, the Zend core automatically determines
A $ a data copy is automatically produced for $ B, and a memory is re-applied for storage.
**/

Echo '$ C = 'abc ';
$ D = & $ c; // $ B points to the memory address of $ a, and the transfer address is assigned a value.
$ D = 'e ';
Echo $ c;
/**
CC
--------------------------------------------------------------------------------
E
**/
?>

<? Php
// Return the function reference
Function & test ()
{
Static $ B = 0; // declare a static variable
$ B = $ B + 1;
Echo '$ B ='. $ B;
Echo '<br> ';
Return $ B;
}
$ A = test ();
Echo '$ a ='. $ a; // This is not a function reference and returned. It is no different from a common function call.
$ A = & test (); // This method is used to return a function reference.
// What is the reference return? (in the PHP Manual, reference return is used when you want to use a function to find the variable on which the reference should be bound .)
// In this example, test () is bound to $.
Echo '$ a ='. $;
$ A = 6;
Test (); // This statement outputs the value of $ B to 7.
Echo '$ a ='. $;
/*
$ B = 1
$ A = 1 $ B = 2
$ A = 2 $ B = 7
$ A = 7
**/
?>

<? Php
// Reference transmission
Function printArray (& $ arr ){
// Echo count ($ arr );
Array_pop ($ arr );
Echo count ($ arr );

}
// Value transfer
Function printArray2 ($ arr ){
// Echo count ($ arr );
Array_pop ($ arr );
Echo count ($ arr );
}
$ A = array (1, 2, 3 );
$ B = $;
// $ B = & $;
PrintArray ($ );
Print_r ($ B );
Print_r ($ );
Echo 'PrintArray ($ );
Print_r ($ B );
Print_r ($ );
/**
2 Array ([0] => 1 [1] => 2 [2] => 3) Array ([0] => 1 [1] => 2)
--------------------------------------------------------------------------------
1 Array ([0] => 1 [1] => 2 [2] => 3) Array ([0] => 1)
**/
?>
<? Php
// Object reference
Class {
Var $ name = "ABC ";
}
$ B = new;
$ C = $ B;
Echo '$ B-> name ='. $ B-> name; // output ABC
Echo '<br> ';
Echo '$ c-> name ='. $ c-> name; // output ABC
Echo '<br> ';
$ B-> name = "DEF ";
Echo '$ B-> name ='. $ B-> name;
Echo '<br> ';
Echo '$ c-> name ='. $ c-> name;
Echo '/*
In PHP5, object replication is implemented through reference.
In the above column, $ B = new a; $ c = $ B; is equivalent to $ B = new a; $ c = & $ B;
In PHP5, objects are called by reference by default,
But sometimes you may want to create a copy of an object,
It is expected that the change of the original object will not affect the copy.
For this purpose, PHP defines a special method called _ clone.
**/
$ X = new;
$ Y = clone $ x;
$ X-> name = 'XXX ';
Echo '$ x-> name ='. $ x-> name;
Echo '<br> ';
Echo '$ y-> name ='. $ y-> name;

/*
$ B-> name = ABC
$ C-> name = ABC
$ B-> name = DEF
$ C-> name = DEF
--------------------------------------------------------------------------------
$ X-> name = XXX
$ Y-> name = ABC
**/
?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.