PHP reference and symbol considerations _ PHP Tutorial

Source: Internet
Author: User
Tags array to string
Discuss some notes about PHP Reference & amp; symbol. PHP referencing symbols is a difficult knowledge point. New users must pay more attention to this point when writing code, because if you have incorrect understanding of the use of PHP reference symbols, it will guide PHP references and symbols as a difficult knowledge point. New users must pay more attention to this when writing code, because if you have an incorrect understanding of the use of PHP references and symbols, the entire code you write will be wrong.

  • How to use PHP to associate an array query result
  • PHP common errors
  • PHP function header (): specific method for querying a single part
  • WordPress won the runner-up of the 2009 Open Source PHP project
  • PHP array to string is related to PHP string to array
Many people misunderstand that the reference in php is the same as the pointer in C. in fact, this is not the case and it is quite different. Pointers in C language must be defined by * except explicit declarations during array transmission, whereas php uses similar pointers to addresses) functions are not implemented by the user, but are implemented by the Zend core. PHP references and symbols use the principle of "copy at Write Time", that is, unless a write operation occurs, variables or objects pointing to the same address are not copied, for example, the following code:

 
 
  1. $a = array('a','c'...'n');
  2. $b = $a;

If the program is only executed here, $ B and $ B are the same, but not like C, $ a and $ B occupy different memory space, it points to the same memory, which is the difference between php and c. it does not need to be written as $ B = & $ a to indicate that $ B points to $ a memory, zend has already helped you implement the reference, and zend will be very intelligent to help you determine when to handle this and when not.

If you continue to write the following code later, add a function, pass the parameter through PHP Reference & symbol, and print the output array size.

 
 
  1. Function printArray (& $ arr) // reference transfer
  2. {
  3. Print (count ($ arr ));
  4. }
  5. PrintArray ($ );

In the above code, we use PHP Reference & symbol to pass the $ a array into the printArray () function. the zend Engine will think that printArray () may cause changes to $, at this time, a $ a data copy is automatically generated for $ B, and a memory is re-applied for storage. This is the "copy at write time" concept mentioned above.

If we change the above code to the following:

 
 
  1. Function printArray ($ arr) // value transfer
  2. {
  3. Print (count ($ arr ));
  4. }
  5. PrintArray ($ );


The above code directly transmits the $ a value to printArray (). at this time, there is no reference transfer, so there is no copy at write time.

You can test the execution efficiency of the above two lines of code, for example, adding a loop 1000 times outside to check the running time, the result will let you know that improper use of PHP references and symbols will lead to a performance reduction of more than 30%.

Bytes. New users must pay more attention to this point when writing code, because if you have an incorrect understanding of the use of PHP reference symbols, it will guide...

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.