How to check if the request is an AJAX request with PHP

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ) 
{
    // This is ajax request.
    // write your code here.
}