As we know, return keyword will RETURN some value and exit the current function. This infers that the proper use of return
would only exist inside of functions/methods.
BUT, I saw some PHP developers use return
outside of functions, even in index.php file (in root of web server). What does it mean in that context? By the way, maybe it's logical to require some file inside function, but this style isn't mine.
There's not much more to say than what the docs do.
About the common usage of return
:
If called from within a function, the
return
statement immediately ends execution of the current function, and returns its argument as the value of the function call. return will also end the execution of aneval()
statement or script file.
About the less common usage:
If called from the global scope, then execution of the current script file is ended. If the current script file was
include
d orrequire
d, then control is passed back to the calling file. Furthermore, if the current script file was included, then the value given to return will be returned as the value of the include call. If return is called from within the main script file, then script execution ends. If the current script file was named by the auto_prepend_file or auto_append_file configuration options in php.ini, then that script file's execution is ended.