[php] How to stop PHP code execution?

Is there a way to immediately stop PHP code execution?

I am aware of exit but it clearly states:

Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.

So what I want to achieve is to stop the PHP code execution exactly when I call exit or whatever.

Any help?

Edit: After Jenson's answer

Trial 1:

function newExit() {
    __halt_compiler();
}    
echo "start";
newExit();    
echo "you should not see this";

Shows Fatal error: __HALT_COMPILER() can only be used from the outermost scope in which was pretty expected.

Trial 2:

function newExit() {
    include 'e.php';
}
echo "start";
newExit();
echo "you should not see this";

e.php just contains __halt_compiler();

This shows startyou should not see this

Edit: Why I want to do this?

I am working on an application that includes a proprietary library (required through virtual host config file to which I don't have access) that comes as encrypted code. Is a sort of monitoring library for security purpose. One of it's behaviours is that it registers some shutdown functions that log the instance status (it saves stats to a database)

What I want to do is to disable this logging for some specific conditions based on (remote IP)

This question is related to php

The answer is


Please see the following information from user Pekka ?

According to the manual, destructors are executed even if the script gets terminated using die() or exit():

The destructor will be called even if script execution is stopped using exit(). Calling exit() in a destructor will prevent the remaining shutdown routines from executing.
According to this PHP: destructor vs register_shutdown_function, the destructor does not get executed when PHP's execution time limit is reached (Confirmed on Apache 2, PHP 5.2 on Windows 7).

The destructor also does not get executed when the script terminates because the memory limit was reached. (Just tested)

The destructor does get executed on fatal errors (Just tested) Update: The OP can't confirm this - there seem to be fatal errors where things are different

It does not get executed on parse errors (because the whole script won't be interpreted)

The destructor will certainly not be executed if the server process crashes or some other exception out of PHP's control occurs.

Referenced in this question Are there any instances when the destructor in PHP is NOT called?


I'm not sure you understand what "exit" states

Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.

It's normal to do that, it must clear it's memmory of all the variables and functions you called before. Not doing this would mean your memmory would remain stuck and ocuppied in your RAM, and if this would happen several times you would need to reboot and flush your RAM in order to have any left.


Apart from the obvious die() and exit(), this also works:

<?php
echo "start";
__halt_compiler();
echo "you should not see this";
?>

You can use __halt_compiler function which will Halt the compiler execution

http://www.php.net/manual/en/function.halt-compiler.php


You could try to kill the PHP process:

exec('kill -9 ' . getmypid());

or try

trigger_error('Die', E_ERROR);