也许这是一个简单而愚蠢的问题.
我们知道,echo
只需打印变量,然后在php函数return
中将函数返回给函数调用者.
我注意到有人在使用echo json_encode
其他人时使用return json_encode
,
我将一些内容返回给jquery,并使用echo/return都可以.
但是当我发现几乎所有人都在使用时echo json_encode
,为什么?
谢谢.
return
在使用ajax的情况下不会给出任何响应,因为它echo
会给你响应.如果你正在使用服务器端脚本,可能你不需要回应它.而不是它你可以使用return.
查看此示例以及调用函数的方式
function myFun($value){ return 'This is the value '.$value; } echo myFun(10);//Output This is the value 10 myFun(10);//Will not give you any output function myFun($value){ echo 'This is the value '.$value; } myFun(10);//Output This is the value 10 echo myFun(10);//Output This is the value 10
在ajax的情况下
$.ajax({ url : 'mypage.php', type : "POST", success : function(res){ alert(res); } });
在mypage.php中
echo 'Hello';//This will send Hello to the client which is the response
但
return 'Hello';//Will not send anything.So you wont get any response