PHP Exit with Status Code

Send HTTP status code from php and exit the page depending on if condition.There will be scenarios where you want to php script execution and return status code to the user. Such a response will not contain response body.

Purpose

There will be scenarios where you want to php script execution and return status code to the user. Such a response will not contain response body.

Avoid access with 401 Unauthorized

Check the user details and halt further checking and exit the page, if the user is unauthorized.Send HTTP status code from php and exit the page depending on if condition

if ($fname.$lname == 'WrongUser'){
    header("HTTP/2 401 Unauthorized");
    exit;
}

Page not modified with 304

There is no need to render the page once the etag has matched, we can simply return 304 and exit the php exeuction.

if ($etagHeader == $etagFile){
  header("HTTP/2 304 Not Modified");
  exit;
}

Both the examples use the guard clause principle