24

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)

ilyes kooli
  • 11,959
  • 14
  • 50
  • 79

7 Answers7

20

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?

Community
  • 1
  • 1
The Humble Rat
  • 4,586
  • 6
  • 39
  • 73
8

whats wrong with return ?

echo "you will see this";
return;    
echo "you will not see this";
vincent thorpe
  • 171
  • 1
  • 10
4

You could try to kill the PHP process:

exec('kill -9 ' . getmypid());
laurent
  • 88,262
  • 77
  • 290
  • 428
3

You can use __halt_compiler function which will Halt the compiler execution

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

Jenson M John
  • 5,499
  • 5
  • 30
  • 46
  • The compiler won't execute the code after '__halt_compiler' but is it really not going to call the destructors and shutdown functions? I didn't check actually but I guess it would call them (can't think of any reason why it wouldn't). – laurent Jan 30 '14 at 13:31
  • Does the compiler still see class/function definitions after that statement? There are differences between compile time and run time in PHP that may be an issue here. – ToBe Jan 30 '14 at 13:32
  • Also note the following, err, note from the docs: `__halt_compiler() can only be used from the outermost scope.` – CompuChip Jan 30 '14 at 13:34
  • 1
    @ToBe According to comment here: http://stackoverflow.com/questions/5270486/whats-halt-compiler-in-php-for `Because PHP would already be running by the time exit is called; __halt_compiler tells PHP to ignore everything after it as if it were inside one big /* comment */` It seems to not see anything – Royal Bg Jan 30 '14 at 13:34
3

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

<?php
echo "start";
__halt_compiler();
echo "you should not see this";
?>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

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.

Arrok
  • 116
  • 5
0

or try

trigger_error('Die', E_ERROR);
user5332
  • 758
  • 2
  • 9
  • 17
  • Should be equivalent of exit() or die() but is also dependant on used error handler. – ToBe Jan 30 '14 at 13:37