打印 JSON 并终止执行以获取更详细的错误报告


Print JSON and terminate execution for more detailed error reports

我想知道是否有任何方法可以在像这样的模具函数中打印 JSON:

die(json_encode($response));  

当然,我已经测试过并且没有按预期工作,因为它需要一个字符串,我不想解析 JSON,我想按原样回显它。

我还考虑过一个自定义函数,例如:

public function reportError ($errorFlag, $message){
  $response = array($errorFlag, $message, $mysqli->error);
  echo json_encode($response);
  die ("");
}  

但我不确定这是否是一个好方法,或者有更简单的方法。我通常很担心好的做法,但我对这个问题迷失了方向。

任何指导不胜感激,提前感谢您。

die()中使用print_r()

print_r()有一个可选的第二个参数,如果设置为 true则以字符串形式返回结果,而不是打印结果。所以这样的事情应该有效:

die(print_r(json_encode($response), true));