Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 'die' and 'exit' [duplicate]

Tags:

php

exit

die

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

like image 473
Yogesh Suthar Avatar asked Oct 03 '12 06:10

Yogesh Suthar


People also ask

What is the difference between ECHO and die?

What is the difference between echo and die() or are they same? The die() function can accept an optional argument string that it will output just before terminating the script. echo() only outputs its arguments, it will not terminate the script.

What does exit () do in PHP?

The exit() function prints a message and terminates the current script.

What is a die statement?

The die() is an inbuilt function in PHP. It is used to print message and exit from the current php script. It is equivalent to exit() function in PHP. Syntax : die($message)


1 Answers

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

like image 173
Adam Plocher Avatar answered Sep 18 '22 05:09

Adam Plocher