How can PHP be used to detect Ajax requests?

To detect Ajax requests in PHP, you can check if the 'HTTP_X_REQUESTED_WITH' header is set to 'XMLHttpRequest'. This header is commonly sent by Ajax requests, so checking for its presence can help identify them.

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    // This is an Ajax request
    // Add your code here to handle the Ajax request
} else {
    // This is not an Ajax request
    // Add your code here to handle regular requests
}