What role does the OPTIONS request play in resolving CORS issues in PHP?

When dealing with CORS (Cross-Origin Resource Sharing) issues in PHP, the OPTIONS request plays a crucial role in resolving them. The OPTIONS request is used to preflight a request, allowing the server to respond with the necessary CORS headers that indicate which origins are allowed to access the server's resources. By handling OPTIONS requests properly in PHP, you can ensure that cross-origin requests are handled securely and without any CORS errors.

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
    header('Access-Control-Allow-Headers: Content-Type');
    header('Access-Control-Max-Age: 86400');
    header('Content-Length: 0');
    header('Content-Type: text/plain');
    http_response_code(200);
    exit;
}