1

I've looked at different answers and except for this one that talks about die while testing and generating errors, I want to know if die() has a negative impact on my code when it is useless to continue executing.

For example, if I have a long script with many conditionals, at a given point, once the condition that I was looking for is successful I don't need my script to keep on testing the other conditions.

$a = 'a';

if($a == 'a'){
 //long script
 die();
}

if($b == 'a'){
 //long script
 die();
}

if($c == 'a'){
 //long script
 die();
}

This is a simple and maybe silly example, with other more elegant solutions, but it helps explain my question; if I die() on the first condition, it will stop from executing the rest of the code, in theory, this should optimize performance, or maybe not...

Is it better to let the script get to the end or die() will actually speed up the process? If die() has negative consequences, of course, there are other ways around it, like creating flags to let the script skip certain chunks of code, I just want to know the negative effects of die() if any.

For example: - In terms of memory, will die() keep using memory or maybe it frees up more memory. - In terms of time/execution will die() speed up a process because it will not try to execute the rest of the script, or it makes no difference at all. Imagine that down the code there might be a heavy process that can be avoided.

I want to be clear about this, I know there must be many other ways to be more efficient, shorter scripts, switches, flags, etc. I just want to understand if there is any negative impact of using die().

Thanks

multimediaxp
  • 9,348
  • 13
  • 49
  • 80
  • I prefer neither.. `exit` is more preferred, but usually in a Controller-based environment (MVC, MVVM) you'd `return` – treyBake Jun 11 '18 at 15:16
  • There are a lot of possible takes on this… Fundamentally you shouldn't have such *"long scripts"*. You should maybe use more functions/OOP. You shouldn't `die` unless you know you're the "top level script", you should prefer `return` or such to give higher callers a chance to continue working. Maybe use exceptions. Probably a better overall control flow. Perhaps a `switch` statement… – deceze Jun 11 '18 at 15:18
  • Negative affect of die() is death of script. And that is only negative if you want it to keep running ? – Andrew Jun 11 '18 at 15:18
  • 1
    Matter of taste I guess. I usually throw an exception, catch it higher, and log the exception. That ends the script. But that is only useful if you want to inspect them later, if you don't care, you might as well exit (or die). – Erwin Moller Jun 11 '18 at 15:18
  • 3
    Is exit really more preferred from die? All I can see from the [die](http://www.php.net/manual/en/function.die.php) man page is `die — Equivalent to exit` Look at all those bytes you save with die! – IsThisJavascript Jun 11 '18 at 15:19
  • I'd recommend you post a more complete example at http://codereview.stackexchange.com. (Read their [requirements for completeness](https://codereview.stackexchange.com/help/on-topic) first!) – deceze Jun 11 '18 at 15:20
  • I think if you write script but not web application you can stop execution by `die` and nothing wrong – Artem Ilchenko Jun 11 '18 at 15:20
  • Is this a CLI script? Or one that will run on a web server? If it's on a web server, you must remember PHP MUST DIE – delboy1978uk Jun 11 '18 at 15:22
  • 1
    This question is not opinion based and should not be put on hold. – evansgambit Jun 11 '18 at 15:32
  • 1
    *"I just want to understand if there is any negative impact of using die()"* - nope ... well, unless you suffer from thanatophobia perhaps. I'd only ever use either for debugging in a test environment mind. – CD001 Jun 11 '18 at 15:47
  • Thank you all, apparently this is really optional and it does not have any negative impact, if any of you would like to give a detailed answer I could get it aproved, or I will create one later with a collection of your comments, I like to have answers for others to read and use, making the question useful. – multimediaxp Jun 11 '18 at 16:07
  • You can use either of them, anyway it is only for testing purpose. – sree Jun 11 '18 at 16:12

1 Answers1

0

The usage of die/exit is a sign of poor code design and it would eventually lead to buggy code. In that sense, it would have negative impact. When exit runs, it terminates the script execution after calling the shutdown function (if shutdown callback function is registered). The only time die/exit produces a positive output if it is used with 0(zero) which terminates the script execution successfully. All the other occurences point to errors. As a result, there is no need to use exit.

Frankly, I should add that if the answer is using exit/die, then either the question is wrong or the script is poorly written. In your example, if a script needs to run depending on a value, then the code should be something like:

abstract class AbstractProcess {
  abstract public function run();
}

class A extends AbstractProcess {
  public function run() { echo 'A'; }
}

class B extends AbstractProcess {
  public function run() { echo 'B'; }
}

class C extends AbstractProcess {
  public function run() { echo 'C'; }
}

class ProcessException extends \Exception { }

class Processor
{
  private $handlers = [];

  public function addProcess($key, AbstractProcess $process) 
  {
    $this->handlers[$key] = $process;
  }

  public function run($val)
  {
    if ( !isset($this->handlers[$val]) )
    {
      throw new ProcessException('Cannot process value: ' .  $val);
    }
    return $this->handlers[$val]->run();
  }
}

This can be more sophisticated. That's not the point. It comes down to having a good exception handling policy. There are hundereds of resources you can find online about Exceptions and how to implement exception handling. Most of the modern frameworks have this built in - all you need is to introduce your exception handling logic. We run the above code in a try/catch block and handle the exception:

try {

  $processor = new Processor();

  $processor->addProcess('a', new A());

  $processor->addProcess('b', new B());

  $processor->addProcess('c', new C());

  $processor->run('5');

} catch ( \Exception $e) {

  if ( $e instanceof ErrorException ) {
    // handle error exception
  }
  if ( $e instanceof ProcessException ) {
    echo $e->getMessage();
  }
  // ..
}

Now, if we exited rather than handling the Exception, then we would be introducing a bug that can be very difficult to find. We may not be even aware of such an 'exit' if the script is running in the background.

evansgambit
  • 825
  • 2
  • 8
  • 9