return

(PHP 4, PHP 5)

return returns program control to the calling module.
Execution resumes at the statement following the called module's invocation.

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 also ends the execution of
an eval() statement or script file.

If called from the global scope, then execution of the current
script file is ended. If the current script file was
included or required,
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.

Note:
Note that since return is a language
construct and not a function, the parentheses surrounding its
arguments are not required. It is common to leave them out, and you
actually should do so as PHP has less work to do in this case.

Note:
If no parameter is supplied, then the parentheses must be omitted
and NULL will be
returned. Calling return with parentheses but
with no arguments will result in a parse error.

Note:
You should never use parentheses around your return
variable when returning by reference, as this will not work. You can
only return variables by reference, not the result of a statement. If
you use return ($a); then you're not returning a
variable, but the result of the expression ($a)
(which is, of course, the value of $a).

Keep in mind that even if PHP allows you to use "return" in the global scope it is very bad design to do so.

Using the return statement in the global scope encourages programmers to use files like functions and treat the include-statement like a function call. Where they initialize the file's "parameters" by setting variables in the global scope and reading them in the included file.

Like so: (WARNING! This code was done by professionals in a controlled environment. Do NOT try this at home!)<?php$parameter1 = "foo";$parameter2 = "bar";$result = include "voodoo.php";?>

This is one of the worst designs you can implement since there is no function head, no way to understand where $parameter1 and $parameter2 come from by just looking at "voodoo". No explanation in the calling file as of what $parameter1 and -2 are doing or why they are even there. If the names of the parameters ever change in "voodoo" it will break the calling file. No IDE will properly support this very poor "design". And I won't even start on the security issues!

If you find yourself in a situation where a return-statement in global scope is the answer to your problem, then maybe you are asking the wrong questions. Actually you may be better off using a function and throwing an exception where needed.

Files are NOT functions. They should NOT be treated as such and under no circumstances should they "return" anything at all.

Remember: Every time you abuse a return statement God kills a kitten and makes sure you are reborn as a mouse!

I believe the way you've explained this for people may be a bit confusing, and your verbiage is incorrect. Your script below is technically calling return from a global scope, but as it says right after that in the description above... "If the current script file was include()ed or require()ed, then control is passed back to the calling file". You are in a included file. Just making sure that is clear.

Now, the way php works is before it executes actual code it does what you call "processing" is really just a syntax check. It does this every time per-file that is included before executing that file. This is a GOOD feature, as it makes sure not to run any part of non-functional code. What your example might have also said... is that in doing this syntax check it does not execute code, merely runs through your file (or include) checking for syntax errors before execution. To show that, you should put the echo "b"; and echo "a"; at the start of each file. This will show that "b" is echoed once, and then "a" is echoed only once, because the first time it syntax checked a.php, it was ok. But the second time the syntax check failed and thus it was not executed again and terminated execution of the application due to a syntax error.