Includes and returns

when you need to include the file, just use the include file, and when you need to return the configuration file, you should use the return include file ...
I usually have a function called " loader ($ file, $ return = false) " where I use $ return to return include, or not.

my question is whether there is a problem that the include include file is returned even for files that are not configuration:

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

Thank you

+4
source share
2 answers

From php.net :

Returns processing: include returns FALSE on failure and raises a warning. Successful includes, if not overridden by the included file, return 1. You can execute the return statement inside the included file to complete processing in this file and return to the script that named it. In addition, it is possible to return values ​​from the included files. You can accept the value of the incoming call as you are for a normal function. However, this is not possible if including deleted files, if the output of the deleted file is not valid PHP start and end tags (as in any local file). You can declare the necessary variables in these tags, and they will depend on which file was included.

If your class/view.php or config/test.php uses return , you can save it. If there is no return in these files, there is no reason if you do not want the current script to continue.

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 ?> 
+4
source

If the included file does not have return , it will simply return 1 , so there will be no problems.

+1
source

Source: https://habr.com/ru/post/1482323/


All Articles