Reference (&) in php

Source: Internet
Author: User
Php references (that is, adding & symbol before variables, functions, and objects) in PHP mean that different names access the same variable content. The pointer in the C language is different from the pointer in the C language. the pointer in the C language stores the address where the variable content is stored in the memory. Variable reference PHP allows you to use two variables to point to the same content & lt ;? Php $ a & quot; ABC & quot; $ B & $ a; echo $ php reference (that is, add & symbol before variables, functions, and objects)

The reference in PHP means that different names access the same variable content.

The pointer in the C language is different from the pointer in the C language. the pointer in the C language stores the address where the variable content is stored in the memory.

Variable reference

PHP reference allows you to use two variables to point to the same content

 

Function address transfer call

I will not talk much about the address transfer call. the following code is provided directly.

 "; Echo $ B; // output 101?>

Note that test (1); will cause an error.

Function Reference return

First look at the code

 

The following explains:

In this way, $ a = test (); is not actually returned by the function reference, which is no different from the normal function call. As for the reason:

This is a php rule. php requires that the function is referenced and returned in the $ a = & test (); method. As for what is 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 .) I haven't understood this sentence for a long time.

The example above is as follows:

$ A = test () method to call a function. it only assigns the value of the function to $, $ a does not affect $ B in the function and calls the function in the $ a = & test () method, the function is to direct the memory address of the $ B variable in return $ B to the same place as the memory address of the $ a variable, this is equivalent to the effect ($ a = & B ;) therefore, changing the value of $ a also changes the value of $ B. Therefore, after $ a = & test (); $ a = 5; is executed, the value of $ B is changed to 5.

Static variables are used to help you understand the reference and return functions. In fact, function reference and return are mostly used in objects.

12 Next page

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.