Php difference in exit (), die (), return () of the Detailed

Recently collected face questions about PHP, which is a question about the exit (), die () function:

About exit () and die () statement is correct:
A, when exit () function performs stop the execution of the following script, and die () can not do
B, when the die () function performs stop the execution of the following script , while exit () can not do
C, with a die () function where you can also replace the function uses the exit ()
D, die () function and exit () function is no different, do exit the program, will stop execute the following script
( the correct answer at the end of the text)

1.exit () and die ()

Definitions and usage, exit (status) and die (status) output function are a message and exits the current script. Both functions are aliases for each, all point to the same function.

When the status is an integer, beginning from version 4.3 will no longer be displayed, if the string type, the output string will be content to abort the page before the script runs.

Let's look at two examples:

$str = "www.baidu.com";
fopen($str,"r") or exit("unable to connect");
// exit函数可以换成die函数,效果一样的。
//运行上面的代码会在页面出现一个报错,同时显示unable to connect字符串
$str = "www.baidu.com";
fopen($str,"r") or exit(96);
// 运行结果:只出现报错,不会显示任何内容。

Summary: die exit and the role and function usage is the same, whether in the main body of a script or function is aborted script runs behind the code is not executed.

2.return

Note: Note that since the structure of the language is not the return function , its argument is not necessary to enclose it in parentheses. Brackets usually they do not, in fact, should not, which can reduce the burden on PHP.

Note: If no parameter, the brackets must not, at this time returns NULL. Plus the brackets but no parameters will result in a parse error if the call return.

Note: When the return value with reference to never use parentheses, this will not work. You can only return variables by reference, not the result of the statement. In fact, instead of returning a variable, but the value of the expression ($ a) (of course, it is also at this time that the value of a $ value); If you use return (¥ a).

If the return statement in a function call, an immediate end to perform this function and its argument as the value of the function's return. return will terminate eval () statement or script file execution.

public test($num)
{
	if($num > 0){
		return "$num大于0";
	}else{
		return;
	}
}

test(100);//返回字符串:100大于0,由于没有任何输出函数,所以此时是没有任何显示内容的。
test(-100);//一样是没有任何显示内容,但是会返回NULL

Summary: return is not a function, should be used when the body of the function, if the return value of the corresponding content returns, otherwise it returns NULL, but will not suspend operation of the main script; if the application in the main script is aborted run postscripts .

Questions answer: C (AB obviously wrong, D major is wrong exit program)

Recently collected PHP questions, welcome to leave a message in the comments area to share.

Released six original articles · won praise 0 · Views 3413

Guess you like

Origin blog.csdn.net/HRG520JN/article/details/104351173