6

In a php script I have some test and after the script the html page.

When a test fail i call die("Test 1 failed"); If no test fail the php script reach the end ?> and then load the html code after the php script.

Is this a good procedure? Or I need to write die() or exit() before the end of php script?

newtphp
  • 245
  • 1
  • 4
  • 13
  • 1
    No. When the script reaches the end, it will exit anyway. – George Dec 16 '13 at 11:35
  • read this :http://stackoverflow.com/questions/1795025/what-are-the-differences-in-die-and-exit-in-php?rq=1 – Karthik Dec 16 '13 at 11:35
  • 1
    It is only useful if someone may be interested in the return code of your script, like when you use it in a shell environment. For most uses, in particular on web servers to generate content, this is not necessary as per the answers below. – CompuChip Dec 16 '13 at 11:37

9 Answers9

4

No you don't have to write that and this is not best practice. If the script reaches the end without fatal errros it will exit.

If this means "testing" for you, you're wrong. Testing should be done using unit tests. For php there is phpunit. Give it a try, that's the proper way of testing your code.

Edit: As CompuChip says in a comment, the only useful use case for exit is when you're writing a php based shell script that should return an error code. See the parameter section of the documentation for the exit() function.

floriank
  • 25,546
  • 9
  • 42
  • 66
  • for test i mean I register the user to a database. If something fails that's a test that fails. – newtphp Dec 16 '13 at 11:37
  • This can and should be tested by unit tests. The way you "test" is debugging, not testing. And instead of "dying" your script should pass a proper error message to the user but not just "kilL" the script. – floriank Dec 16 '13 at 11:39
0

You should never be using die() or exit in your production PHP scripts except in very specific cases. Instead, re-work your code paths to simply show an error message to the user rather than exiting the script early.

John Dorean
  • 3,744
  • 9
  • 51
  • 82
0

No ! This is not recommanded to use it

Use trigger_error or error_log to log the tests in your error.log. Then check it.

David Ansermot
  • 6,052
  • 8
  • 47
  • 82
0

No you don't need that, but when writing console PHP scripts, you might want to check with for example Bash if the script completed everything in the right way. That's when you use exit() or die()

LightningBoltϟ
  • 1,383
  • 2
  • 10
  • 28
0

Is the die() or exit() function needed in the end of a php script?

No, PHP will end the script itself. If the script is an included file (called from another file) then it will end script in the included file and then continue with any code in the original file after where you included (if there is any code).

So you put die() or exit() where ever you want or need it.

For testing, put it after each block of code you test. I use them in some parts of testing if I just want PHP to show me something then stop, such as print out an array to make sure it's being constructed correctly etc.
eg:

print_r($array);
exit();

For other code tests, I sometimes just echo "Section A worked", etc, such as within if/else. If I want to know if a particular part of code is working or if some criteria is being met or not (basically, it lets you trace where PHP itself is going within your code).

All that said, don't use die() or exit() in production code. You should use a more friendly and controlled messaging setup. For security reasons and visual, as you could potentially give them some info like "ERROR Failed to load SomethingSecret". Also it doesn't look pretty when you page only half loads and then puts out an on screen error message which likely means nothing to the end user.

Have a read through this:
PHP Error handling: die() Vs trigger_error() Vs throw Exception

Community
  • 1
  • 1
James
  • 4,644
  • 5
  • 37
  • 48
-1

No you don't have to use these functions at the end of the script, because it exists anyway at the end of the script.

Maarkoize
  • 2,601
  • 2
  • 16
  • 34
-1

No need to put a die or an exit at the end of the scipt. But you may use exit to terminate your script with a specific exit code (by default it's 0).

E.g

$ php -r "/* does nothing */;"
$ echo $?
0
$ php -r "exit(123);"
$ echo $?
123

http://php.net/exit

voondo
  • 2,533
  • 1
  • 16
  • 21
-1

From the documentation:

The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close().

Leo
  • 10,407
  • 3
  • 45
  • 62
-1

Nope, you don't need to call die() or exit(0 if you have another code to run, like you HTML code