Function : Response

The response function is an http_response_code driven json string that is built for handling ajax responses. It takes an http response code as argument and uses it to build a json response string. By default, all 4xx and 5xx response codes when supplied as argument are considered as errors while other response codes are considered as successes. This behaviour sets the json error key as true when any of the error codes are supplied as argument. These can be overidden by supplying a third boolean argument that defines the success status of the response. When error is set as true, success becomes false and vice versa.

A defined response code of 200 will be as shown below:
  response(200, 'successful'); 
                  
  { 
    "success":true,
    "error"  :false,
    "message":"successful",
    "response_code":200
  }
                  
A defined response code of 500 will be as shown below:
  response(500, 'failed'); 
                  
  { 
    "success":false,
    "error"  :true,
    "message":"failed",
    "response_code":500
  }
                  
If ever there is any reason to customize the response in such a way that the default behavior is overidden, this can be done by supplying the third argument as true or false. A true value will assume that the response code is success while a value of false will assume that the response is error.
  response(500, 'successful', true); 
                  
  { 
    "success":true,
    "error"  :false,
    "message":"successful",
    "response_code":500
  }
                  
Although, the header code of the current page will be automatically set when this function is used, we can obtain the json response in the following way.
  $response = response(500, 'successful', true); //set header and return json
                  
Lastly, note that the error key's value is usually the inverse of the success key's value. So, when success is true, then error will be false.