Home > Backend Development > PHP Tutorial > Detailed code examples of the difference between echo and return in PHP

Detailed code examples of the difference between echo and return in PHP

PHP中文网
Release: 2023-03-01 06:16:01
Original
1561 people have browsed it

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);
        }
    })
Copy after login
$addID = $this->input->post('addId');
        $data = $this->Address_model->getAddressListInfo($addID);
        (1)echo json_encode($data);
        (2)return json_encode($data);
Copy after login

You can use echo above, but not return! Solution

Reply content:

echo: used to output results
return: return processing results

There is no output result, how does js receive it?

return is the return value in function, which is convenient for subsequent calls

sure Can not be done.
return is the execution result set of the returned method, and there is no output
echo is to output this json
ajax json object You need to get the string

of the parsing output. It is very simple. Suppose you are a json parsing machine yourself. After your code runs and then returns, nothing will be displayed on the browser
. Only by outputting the result can your eyes see it and you guess it can be parsed

return is not returned to js...
Think about it, PHP The return value of the language function is returned to javascript...
This reminds me of a newbie I brought in before who wanted to throw PHP exceptions to the browser...

return is to return results to the PHP program.
echo is the response of Ajax.

echo means outputting the result to the requester, and return means returning the value to the caller without outputting the result. Ajax obtains data based on page values.

The above is a detailed code example of the difference between echo and return in PHP. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template