Php 返回字符串,但不返回';t回音到屏幕上

Php 返回字符串,但不返回';t回音到屏幕上,php,return,Php,Return,我试图理解return关键字及其功能,但是,我有点困惑 我读过这本书,这是一个很好的起点,但我似乎无法理解我所想象的一个非常简单的问题的解决方案 假设我有一个名为index.PHP的PHP文件,我从中调用一个函数,就像这样(假设它不是从类或函数中调用的) 然后在我的function.php中,我有: public function editAnswer($answer_id, $answerString, $correct, $questionID, $lecturer_id){ $

我试图理解
return
关键字及其功能,但是,我有点困惑

我读过这本书,这是一个很好的起点,但我似乎无法理解我所想象的一个非常简单的问题的解决方案

假设我有一个名为index.PHP的PHP文件,我从中调用一个函数,就像这样(假设它不是从类或函数中调用的)

然后在我的function.php中,我有:

public function editAnswer($answer_id, $answerString, $correct, $questionID, $lecturer_id){

     $db = $this -> db;


     if (!empty ($answer_id) && !empty ($answerString) && !empty($correct) && !empty($questionID) && !empty ($lecturer_id)){


         $result = $db -> editAnswer($answer_id, $answerString, $correct, $questionID, $lecturer_id);

         if ($result) {

           return "The Updates To Your Answer Have Been Saved!";


         } else {

            $response["result"] = "failure";
            $response["message"] = "The Updates To Your Answer Have Not Been Saved, Please Try Again";
            return json_encode($response);

         }

   } else {

      return $this -> getMsgParamNotEmpty();

   }
}
如果成功,我将返回字符串
您的答案更新已保存,但是,这将立即在index.php的屏幕上显示

我的想法是,如果在调用函数后,用户仍然在index.php上,那么在页面顶部有一个字符串可能看起来有点难看。也许我希望捕获返回的字符串,然后在显示它之前创建一个警报对话框


我如何做这样的事情,而不是立即回显返回的字符串?

回显返回值,因为您使用
echo
输出结果

将结果保存到变量中,以后再使用

$result = $fun -> editAnswer ($obj->answer_id, $obj->answerString ,$obj->correct, $questionID, $lecturer_id);

echo "we called the function"
echo "the result was $result"

我不确定我是否完全理解您的意思,但是您可以将方法“response”分配到一个变量中,例如$temp=$fun->editAnswer($obj->answer\u id,$obj->answerString,$obj->correct,$questionID,$concert\u id);然后在页面中的任何其他位置使用$temp变量。将返回的值保存在变量中,或者仅在实际需要时调用并回显该值。我不明白这个问题谢谢大家-这是一个简单的解决方案,我只是有一个有趣的5分钟。
$result = $fun -> editAnswer ($obj->answer_id, $obj->answerString ,$obj->correct, $questionID, $lecturer_id);

echo "we called the function"
echo "the result was $result"