Sample code for echo and return in PHP

Source: Internet
Author: User
What is the difference between echo and return in returning ajax results? {Code...} can be used with echo above, but not with return! What is the difference between echo and return in returning ajax results?

$.ajax({        ...        success: function (data) {            addType = "change";            var obj = JSON.parse(data);            console.log(data);        }    })
$addID = $this->input->post('addId');        $data = $this->Address_model->getAddressListInfo($addID);        (1)echo json_encode($data);        (2)return json_encode($data);

The above can be done with echo, but not with return! Solution

Reply content:

Echo: Used to output results
Return: returns the processing result.

No output results. how does js receive them?

Return is the return value in the function to facilitate subsequent calls.

No.
Return is the execution result set of the return method, and no output is made.
Echo is to output this json
Ajax json object needs to obtain the parsed output string

It is easy to say that you are a json parsing machine. after your code is run and then return, nothing is displayed on the stray machine.
Only by outputting the results will your eyes be able to understand what you can guess.

Return is not returned to js ......
Think about the function in PHP, the return value is returned to javascript ......
This reminds me of a new person who used to bring PHP exceptions to the browser ......

returnReturns results to the PHP program.
echoIs the Ajax response.

Echo indicates that the output result is sent to the requester, and return indicates that the returned value is not returned to the caller. Ajax obtains data based on the page value.

The above is a detailed description of the differences between echo and return code instances in PHP. For more information, see PHP Chinese network (www.php1.cn )!

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.