Php references can only be defined in function parameters. transferring references in real time is forbidden.

Source: Internet
Author: User
{Code...} which scenarios are applicable to the above two situations respectively?
14. the reference can only be defined in function parameters. it is forbidden to pass the reference in real time. For example: // reference defined in function parameters-allowed function defineRefInMethod (& $ a) {$ a = 'a';} defineRefInMethod ($ B); echo $ B; // 'A' // pass the reference in real time-the function callTimePassRef ($ a) {$ a = 'a';} callTimePassRef (& $ c); echo $ c; // 'A'

Which scenarios are applicable to the above two situations?

Reply content:
14. the reference can only be defined in function parameters. it is forbidden to pass the reference in real time. For example: // reference defined in function parameters-allowed function defineRefInMethod (& $ a) {$ a = 'a';} defineRefInMethod ($ B); echo $ B; // 'A' // pass the reference in real time-the function callTimePassRef ($ a) {$ a = 'a';} callTimePassRef (& $ c); echo $ c; // 'A'

Which scenarios are applicable to the above two situations?

Sometimes, we process arrays like this.

$arr = [...];foreach($arr as &$value){    // do something here...}print_r($arr);

1. the former is the data transfer address, and the latter is the data transfer value. The former generates global changes for & modified variables in the function body, while the latter does not. A function can have only one return value. Generally, the return value is used when a single variable is changed. you can only use & when changing multiple variables &.
2. references can only be defined in function parameters. This sentence indicates suspicion, does not know where the source is, and the last half of the sentence is incredible.

In fact, the second method will report an error.

If no error is reported, it is also a wonderful way of writing, because the address is read-only and cannot be written.

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.