tags:

views:

72

answers:

4

Hello all,

I would like to know in the following case which is a better solution:

In the PHP script, if the fileSize is larger than 100 then I stop the script.

<?php
...
if ($fileSize > 100) // case I
{
   $results['msg'] = 'fileSize is too big!';
   echo json_encode($results);
   exit();
}

?>

<?php
...
if ($fileSize > 100) // case II
{
   $results['msg'] = 'fileSize is too big!';
   exit( json_encode($results) );
}

?>

<?php
...
if ($fileSize > 100) // case III
{
   $results['msg'] = 'fileSize is too big!';
   return( json_encode($results) );
}

?>

which is a best solution?

Thank you

+3  A: 

Since you are using exit and return within the global scope (not inside a function), then the behavior is almost the same.

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).

Aziz
+1  A: 

I would tend to go with the return() method, so that other scripts can continue executing. That way, if you ever use another script to call this one, it can do error-handling to deal with the case where the file is too large, as opposed to always halting execution.

JGB146
+1  A: 

Exit terminates the program like die(). manual

Iznogood
+1  A: 

It depends...if your script is intended to do nothing else but output a message, and you don't want the script to do anything afterwards, exit() will work. Otherwise, use return.

Crayon Violent