function echo_json_and_die($data, $status_code)
{
http_response_code($status_code);
echo json_encode($data, JSON_PRETTY_PRINT);
die();
}
echo_json_and_die
([
'status' => 'error',
'text_message' => 'Could not find that resource.',
'html_message' => '<p class="error-message">Could not find that resource.</p>',
], 404);
function close_connection_but_continue_processing($output)
{
if (function_exists('fastcgi_finish_request'))
{
echo $output;
ignore_user_abort(true); //https://bugs.php.net/bug.php?id=68772
fastcgi_finish_request();
}
else
{
ob_start();
echo (empty($output) ? ' ' : $output); //Quirk: At least one character must be outputted for the connection to be closed
header('Content-Length: ' . ob_get_length());
header('Connection: close');
ob_end_flush();
ob_flush();
flush();
session_write_close();
}
}
close_connection_but_continue_processing('Your request has been received and will now be processed.');
do_long_task();
function str_contains_any($haystack, $needles, $case_sensitive)
{
foreach ($needles as $needle)
{
if (str_contains($haystack, $needle) || (($case_sensitive === false) && str_contains(strtolower($haystack), strtolower($needle))))
{
return true;
}
}
return false;
}
$haystack = 'This is a load of shizzle';
$needles = ['fudge', 'shizzle'];
$match_found = str_contains_any($haystack, $needles, true); //true