4

when you need to include a file, simply use "include file" and when you need to return a configuration file you must use "return include file"...
usually i have a function "loader( $file , $return = false )" where I use $return to return include, or not.

my question is if there is a problem in keeping return include file even for files that are not configuration:

return include "class/view.php"
return include "config/test.php"

thank you

Papa Charlie
  • 625
  • 8
  • 31

2 Answers2

5

From php.net:

Handling Returns: include returns FALSE on failure and raises a warning. Successful includes, unless overridden by the included file, return 1. It is possible to execute a return statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would for a normal function. This is not, however, possible when including remote files unless the output of the remote file has valid PHP start and end tags (as with any local file). You can declare the needed variables within those tags and they will be introduced at whichever point the file was included.

If your class/view.php or config/test.php uses return, then you may keep it. If there is no return in those files, there is no reason, unless you want to prevent current script from further execution.

Example 1:

<?php
echo 1;  // < executes

return include 'somefile.php';  // < script will end here because of "return"

echo 2;  // < not executes ever
?>

Example 2:

<?php
echo 1;  // < executes

include 'somefile.php';  // < executes

echo 2;  // < executes
?>
BlitZ
  • 12,038
  • 3
  • 49
  • 68
  • even if I run include within a class-function, a file with a return break the chain that could be sequential... nothing would be done after the include, then I think the best choice would be to keep an import classes and import configuration files to where I could handle until the types with more facilities (.php , .ini, ...)correct? cheers to you too friend – Papa Charlie May 23 '13 at 07:41
  • @PapaCharlie return within function context will not break script execution. Return within script context will. Development choises is up to you. – BlitZ May 23 '13 at 07:51
1

If included file doesn't have return, it will just return 1, so there will be no problem.

Maxim Khan-Magomedov
  • 1,326
  • 12
  • 15