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.
response(200, 'successful');
{
"success":true,
"error" :false,
"message":"successful",
"response_code":200
}
response(500, 'failed');
{
"success":false,
"error" :true,
"message":"failed",
"response_code":500
}
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
}
$response = response(500, 'successful', true); //set header and return json
error key's value is usually the inverse of the success key's value.
So, when success is true, then error will be false.