exit, exit(), exit(0), die(), die(0) - How to exit script

后端 未结 7 1959
醉梦人生
醉梦人生 2020-12-24 00:10

I believe that all of these (and even die() or die(0)) are identical. If they are not identical, which is preferred for exiting a script successfu

7条回答
  •  盖世英雄少女心
    2020-12-24 00:33

    I will get downvoted to hell, but in some cases when hacking in CLI, we do not want the program to get killed, while not wanting to continue the full execution.

    Here the goal is to avoid making api calls to a separate hand-point file. Say I have a nice play button in my interface, that execute system calls.

    Example 1: The program get killed after the job , no datas returned. This is not wanted.

    if ($_GET["play"] != ""){
      // Some job
      exit; 
    }
    /* Huge amount of data*/ 
    

    Example 2: The program still runs, feeding the whole data in the GET request. This is unnecessary on this case. This is slowing down the browser with all the data, that he has already.

    if ($_GET["play"] != ""){
      // Some job
    }
    /* Huge amount of data*/ 
    

    Example 3: The program still runs, no data returned as expected, the play command had been executed, but the whole data set get parsed, this is unnecessary job, can slow down php/the machine.

    /* Huge amount of data*/ 
    if ($_GET["play"] != ""){
      // Some job
    }
    

    Example 4: The program still runs, no data returned as expected, the play command had been executed, the whole data had not been parsed, php returned super quickly 200OK with an empty response, as expected. Everyone happy!

    if ($_GET["play"] != ""){
      // Some job
      goto END;
    }
    /* Huge amount of data*/
    END;
    

    Yes! Using GOTO, sometimes is to be considered -as the best to do

提交回复
热议问题