Be aware that this function will be called even id the connection with the client has been aborted or timed out and in fact that's why this approach could be very useful in some cases.
<?php
function YourFunction()
{
...
}
register_shutdown_function("YourFunction");
?>