在jQuery ajax调用方面,php echo和return之间的区别

时间:2021-04-29 22:26:37

I was having trouble getting a jQuery Ajax call's success function to work properly and it was pointed out to me that the reason was that my PHP function was using return $result when I should be using echo $result.

我无法让jQuery Ajax调用的成功函数正常工作,并且向我指出原因是我的PHP函数在使用echo $ result时使用了返回$ result。

Changing the PHP function that the Ajax called from "return $result" to "echo $result" fixed the problem, but why? There's loads of explanations as to the difference between the two (return and echo) in terms of PHP scripts, but how do they differ when sending that value to an Ajax call?

将Ajax从“返回$ result”调用的PHP函数更改为“echo $ result”修复了问题,但为什么呢?关于PHP脚本的两者(返回和回声)之间的区别有很多解释,但是在将该值发送到Ajax调用时它们有何不同?

4 个解决方案

#1


21  

Well, the ajax call reads the response from the server, and that response must be rendered as some type of readable data, such as application/json or text/html.

好吧,ajax调用从服务器读取响应,并且该响应必须呈现为某种类型的可读数据,例如application / json或text / html。

In order to write that data, you need to echo it from the server with PHP.

为了编写该数据,您需要使用PHP从服务器回显它。

The return statement doesn't write data, it simply returns at the server level.

return语句不会写入数据,它只是在服务器级别返回。

#2


8  

An Ajax call uses the response of an HTTP request. A PHP script doesn't generate output by returing, but by echoing.

Ajax调用使用HTTP请求的响应。 PHP脚本不会通过returing生成输出,而是通过echoing生成输出。

#3


5  

Ajax calls see data the same way that we do, it reads it as a string. It's basically accessing another web page and "receiving" the result. PHP's 'return' is returning the value on the server only. You need to actually output the data so that when the Ajax call is made, the page it is looking at actually has data written out.

Ajax调用以与我们相同的方式查看数据,它将其作为字符串读取。它基本*问另一个网页并“接收”结果。 PHP的'return'仅返回服务器上的值。您需要实际输出数据,以便在进行Ajax调用时,它正在查看的页面实际上已写出数据。

#4


2  

The echo command outputs data to the Standar Output, this is in web browser applications, the client who requested the data. In CLI this print the data on the console. And return command exit the function with a value, but doesn't print anything.

echo命令将数据输出到Standar Output,这是在Web浏览器应用程序中,即请求数据的客户端。在CLI中,这将在控制台上打印数据。并且return命令使用值退出函数,但不打印任何内容。

If you want to comunicate between PHP functions, you have to use return. But if you want to output some data, you have to use echo.

如果要在PHP函数之间进行通信,则必须使用return。但是如果要输出一些数据,则必须使用echo。

#1


21  

Well, the ajax call reads the response from the server, and that response must be rendered as some type of readable data, such as application/json or text/html.

好吧,ajax调用从服务器读取响应,并且该响应必须呈现为某种类型的可读数据,例如application / json或text / html。

In order to write that data, you need to echo it from the server with PHP.

为了编写该数据,您需要使用PHP从服务器回显它。

The return statement doesn't write data, it simply returns at the server level.

return语句不会写入数据,它只是在服务器级别返回。

#2


8  

An Ajax call uses the response of an HTTP request. A PHP script doesn't generate output by returing, but by echoing.

Ajax调用使用HTTP请求的响应。 PHP脚本不会通过returing生成输出,而是通过echoing生成输出。

#3


5  

Ajax calls see data the same way that we do, it reads it as a string. It's basically accessing another web page and "receiving" the result. PHP's 'return' is returning the value on the server only. You need to actually output the data so that when the Ajax call is made, the page it is looking at actually has data written out.

Ajax调用以与我们相同的方式查看数据,它将其作为字符串读取。它基本*问另一个网页并“接收”结果。 PHP的'return'仅返回服务器上的值。您需要实际输出数据,以便在进行Ajax调用时,它正在查看的页面实际上已写出数据。

#4


2  

The echo command outputs data to the Standar Output, this is in web browser applications, the client who requested the data. In CLI this print the data on the console. And return command exit the function with a value, but doesn't print anything.

echo命令将数据输出到Standar Output,这是在Web浏览器应用程序中,即请求数据的客户端。在CLI中,这将在控制台上打印数据。并且return命令使用值退出函数,但不打印任何内容。

If you want to comunicate between PHP functions, you have to use return. But if you want to output some data, you have to use echo.

如果要在PHP函数之间进行通信,则必须使用return。但是如果要输出一些数据,则必须使用echo。