我试图找到最简单的可能场景来隔离我的 rest API 不返回任何值的问题,所以这里是:我的服务器上有一个 test.php 文件,其中包含以下内容:然后我使用唯一的标头Content-Type: application/json`"发出一个简单的请求https://www.eswys.ch/tmp/test.php?t1=testhttps://www.eswys.ch/tmp/test.php?t2=test结果分别是测试...""我真的很难理解这一点,为什么我的返回值不知何故丢失"了 - 对此有什么解释吗?! 解决方案 从函数返回实际上并没有向响应呈现任何内容.您可能已经看到人们在不同的框架中返回数据,但框架实际上是在幕后为您回显数据.I am a newbie in php and rest and I'm trying to understand what is going on in my API when using echo vs return....I've tried to get down to the simplest possible scenario to isolate an issue of my rest API not returning any value, so here is goes:I have a test.php file on my server with following content:<?php if(function_exists($_GET['t1'])) { echo $_GET['t1'](); } else if(function_exists($_GET['t2'])) { return $_GET['t2'](); } function test() { return json_encode("test..."); }?>I then make a simple request using, as unique header 'Content-Type: application/json`https://www.eswys.ch/tmp/test.php?t1=testhttps://www.eswys.ch/tmp/test.php?t2=testAnd results are, respectively"test..."""I really struggle to understand this, why is my returned value somehow "lost" - is there any explanation to this?! 解决方案 Returning from a function does not actually render any content to the response. You may have seen people returning data in different frameworks but the framework is actually echoing the data for you behind the scenes. 这篇关于休息回声与 GET 中的返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 18:46