PHP-退出或返回哪个更好? [英] PHP - exit or return which is better?
问题描述
在以下情况下,我想知道哪种方法更好:
I would like to know in the following case which is a better option:
在PHP脚本中,如果$fileSize
变量大于100,我将停止脚本;
In the PHP script, if the $fileSize
variable is larger than 100, I stop the script;
案例1:
<?php
if ( $fileSize > 100 )
{
$results['msg'] = 'fileSize is too big!';
echo json_encode( $results );
exit();
}
第二种情况:
<?php
if ( $fileSize > 100 )
{
$results['msg'] = 'fileSize is too big!';
exit( json_encode( $results ) );
}
情况III:
<?php
if ( $fileSize > 100 )
{
$results['msg'] = 'fileSize is too big!';
return( json_encode( $results ) );
}
上面的三(3)个选项中哪一个是最好的?
Which of the three (3) options above is the best?
推荐答案
由于您是在全局范围内(而不是在函数内)使用exit
和return
,因此行为几乎是相同的.
Since you are using exit
and return
within the global scope (not inside a function), then the behavior is almost the same.
如果通过include()
或require()
调用文件,则会出现这种情况下的区别. exit
将终止程序,而return
将控制权移回到调用脚本(在其中调用了include
或require
的脚本).
The difference in this case will appear if your file is called through include()
or require()
. exit
will terminate the program, while return
will take the control back to the calling script (where include
or require
was called).
这篇关于PHP-退出或返回哪个更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!