PHP in , There are three ways to terminate a script : Used in the main script program return, Use in scripts exit(),die(), The script files in which they are located are not executed .

1. die( )  and  exit( )

stay PHP in ,die() and exit() The usage and function of the is the same . 

These two functions can have parameters , If the parameter is a string , The function outputs a string before aborting the script .

If the parameter is an integer , This value is used as the exit state . The range of values is 0 reach 254 between .

Exit status 255 from PHP retain , Will not be used ; state 0 Used to successfully terminate the program .

notes :PHP Version number is greater than 4.2.0, The values in brackets are integers , This parameter is not output .

because die() and exit() The usage and function of , I'll take it die() Let's take an example .

Code example 1:

  In the main script :
<?php header(content-type:text/html;charset=utf-8); $a = 1; $b = 9; echo $a;
// output 1 die; // Abort script run , The following is not running $sum = $a + $b; echo $sum; // Not output ?>
Operation results : 
1
Code example 2:

  in function :
<?php header("content-type:text/html;charset=utf-8"); function A(){ echo "2";
// output 2 die("error"); // Abort script run , And output the string in brackets } echo "1"; // Output first 1 A(); // Call function
echo " Money is hard to come by "; // Not output ?>
Operation results :
1 2 error
Explain the example 2:" Money is hard to come by " Not output ?

* first PHP The implementation mechanism is from top to bottom .
* The first step is to declare the function
* Step 2 echo Statement output 1
* The third step is to call the function A, output 2, implement die   Abort script run And output error
*
Summary : die( ) and exit( ) Whether in the main script or function, the script is stopped , The rest of the code is not executing .

2. return

first return It's language structure , It's not a function . It can have a return value .

If called in the main script , The current script file stops running . If the current script file is include Or require Of , The control returns the call file . in addition , If the current script is
include  Of , be return The value of is treated as include The return value of the call , Accordingly, a variable is defined in the main script file to receive the return value .

If a function contains return
sentence , When this function is called , The function is executed only until return This step ,return None of the subsequent statements will be executed , And will return Is returned as the value of the function .

Code example 1:

  In the main script :
<?php header("content-type:text/html;charset=utf-8"); $name = " Money is hard to come by "; echo
" The Red Army is not afraid of expeditions "; // output return; // Abort script run echo $name; // Not output ?>
Operation results :
The Red Army is not afraid of expeditions
Code example 2:

  In function body :
<?php header("content-type:text/html;charset=utf-8"); function A(){ $a = 5; $b
= 4; // Define two local variables return $a + $b; // return $a+$b Value of , And ends the current script echo $a; // $a No longer output }
$sum = A(); // Call function And receive with variable return Return value of echo $sum; // output $sum echo " Money is hard to come by "; // output
" Money is hard to come by " ?>
  Operation results :
9 Money is hard to come by
*
Summary : Call in main script return , The script stops running . Call in function body return, Return if there is a return value , The code in the function is not executing . The main script code will still run .

Technology