Difference between 'die' and 'exit' [duplicate]
Asked Answered
M

5

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...

Masha answered 3/10, 2012 at 6:20 Comment(3)
As @AdamPlocher said, they are one and the same. Use witch one you like best.Meitner
I've actually only seen them used other way around... "or die(..)" etc :) but like said, there's no differenceNumeral
mysql functions will be deprecated, please use PDO or mysqli;Hundredth
H
17

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

Hinrichs answered 3/10, 2012 at 6:21 Comment(5)
thanks for answering . But can you tell me why PHP lang. developers created these two function. We can use exit everywhere.Masha
Maybe the die function call was created to make Perl programmers feel at homeAmygdalate
@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.Numeral
The die alias also makes for fun lines of code like comply() or die();Bonne
@darvids0n enter_the_dragon() or exit(); ;)Alible
A
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

Alible answered 3/10, 2012 at 6:38 Comment(3)
+1 for finding at-least 1 difference.Masha
It's still not a difference between the two functions. It's just that usually people use die with a message and exit with an exit code.Dougall
+1 to ThiefMaster. You can change the last line to die(1); without any changes to the behavior.Stunning
O
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 :)

Oslo answered 3/10, 2012 at 6:35 Comment(1)
If you are citing deprecated aliases, please include examples.Finsteraarhorn
R
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.

Rev answered 3/10, 2012 at 6:31 Comment(0)
D
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);
}
Ddt answered 3/10, 2012 at 7:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.