15

Possible Duplicate:
what are the differences in die() and exit() in PHP?

I am totally confused in the difference of die and exit.

Most programmers use die like this.

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');  //don't see mysql_* problem it is just example
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

and using exit like this

$filename = '/path/to/data-file';
$file = fopen($filename, 'r')
   or exit("unable to open file ($filename)");

According to there functionality , I don't think so there is any difference because both terminates the execution of the script.

My question is

1) Can I interchange die with exit and vice-versa in these examples?

2) And the difference between these also.

Cheers...

Community
  • 1
  • 1
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100

5 Answers5

17

According to Die it is Equivalent to exit. So yes, you can interchange them .

Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
Adam Plocher
  • 13,994
  • 6
  • 46
  • 79
  • thanks for answering . But can you tell me why `PHP` lang. developers created these two function. We can use `exit` everywhere. – Yogesh Suthar Oct 03 '12 at 06:25
  • 1
    Maybe the die function call was created to make Perl programmers feel at home – asprin Oct 03 '12 at 06:28
  • 2
    @YogeshSuthar PHP is filled with aliases like that. Some of it is planned, but I would claim most of it is not. It's just been evolving and changing as time goes by and some turn out to be worse ideas than others. – eis Oct 03 '12 at 06:38
  • 8
    The `die` alias also makes for fun lines of code like `comply() or die();` –  Oct 03 '12 at 07:35
  • 2
    @darvids0n `enter_the_dragon() or exit();` ;) – Prasanth Oct 03 '12 at 11:16
13

When using command line,

die("Error");

Will print to "Error" to STDOUT and exit with error code 0.

if you want to exit with error code 1, you have to:

fwrite(STDERR, "Error");
exit(1);

It could be useful while executing php scripts from command line or shell scripts and you want to see if the script terminated with a non zero exit code.

That is one difference I could think of.

P.S. Above info obtained from php.net/exit

Prasanth
  • 5,230
  • 2
  • 29
  • 61
8

There is no difference between die() and exit() function. They both are same and worked same.

Again question is why php keep the both functions if they are same. Both functions are alias of each other function.

Due to API and keeping the backward compatibility both functions are kept.

Here is one more example:

is_int() and is_integer() are also same.

There are quite a few functions in PHP which you can call with more than one name. In some cases there is no preferred name among the multiple ones, is_int() and is_integer() are equally good for example. However there are functions which changed names because of an API cleanup or some other reason and the old names are only kept as aliases for backward compatibility. It is usually a bad idea to use these kind of aliases, as they may be bound to obsolescence or renaming, which will lead to unportable script. This list is provided to help those who want to upgrade their old scripts to newer syntax.

Full list of Aliases function you will find on following URL:

http://php.net/manual/en/aliases.php

May this will help you :)

drsndodiya
  • 1,685
  • 1
  • 17
  • 36
5

die is alias of exit function.

There are many function aliases in php, due to how the language has evolved, evolve and get over it as well - http://www.php.net/manual/en/aliases.php.

Justin John
  • 9,223
  • 14
  • 70
  • 129
5

die prints argument to STDOUT, not to STDERR (grep or 2>/dev/null will help you to test it) die returns to shell exit code as 0, but exit can return other code lets define die full analog in PHP:

function mydie($str){
  echo $str.PHP_EOL;
  exit(0);
}
Maxim Antonov
  • 295
  • 3
  • 9