About references (&) in PHP

Source: Internet
Author: User

PHP references (add & symbols before variables or functions, objects, etc.)

The reference in PHP means: Different names access the same variable content.

There is a difference between the pointers in the C language. The pointer inside the C language stores the address where the contents of the variable are stored in memory.

A reference to a variable

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

 
  

Invocation of a function's address

Address call I'm not going to say much. Directly below the code

 
  "; echo $b;//Output 101?>

It is important to note that here Test (1), the words will be wrong, reason to think.

A reference to the function returns

Look at the code first

 
  

The following explanation:

In this way $a=test (); The result is not a function reference return, which is no different from the normal function call, as for the reason:

This is the PHP rule, PHP rules through $a=&test (); The way to get is to return the reference to the function. As for what is a reference return (the PHP manual says that reference return is used when you want to use a function to find out which variable the reference should be bound to.) I didn't read this bullshit.

This is explained by the example above:

$a =test () call the function, just assign the value of the function to $ A, and $ A does nothing to affect the $b in the function and call the function by $a=&test (), his function is to return the memory address of the $b variable in the $b with the The memory address of a variable points to the same place, which produces the equivalent effect ($a =&b;) So changing the value of $ A also changes the value of the $b so that it executes $a =&test (); $a = 5; Later, the value of the $b becomes 5

This is to let everyone understand that the function reference returns only use static variables, in fact, the function of the reference return is used in the object.

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.