PHP-退出或返回哪个更好? [英] PHP - exit or return which is better?

查看:82
本文介绍了PHP-退出或返回哪个更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下情况下,我想知道哪种方法更好:

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?

推荐答案

由于您是在全局范围内(而不是在函数内)使用exitreturn,因此行为几乎是相同的.

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将控制权移回到调用脚本(在其中调用了includerequire的脚本).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆