PHP 5.4.36 Released

return

(PHP 4, PHP 5)

return は、プログラムの制御を呼び出し元に戻します。 呼び出し側のモジュールでは、呼び出しの次の文から続行します。

関数内で呼び出されると、return文は即座に その関数の実行を停止し、引数を関数の値として返します。 returnはまた、eval()文や スクリプト自体の実行を終了させることが出来ます。

グローバルスコープで呼び出されると、現在実行中のスクリプトが終了 します。もしそのスクリプトが include もしくは require されたものである場合、制御は呼び出し元 のファイルに戻ります。また、そのスクリプトが include されたものである場合は、returnに与えられた引数 の値は include の戻り値となります。 returnがメインスクリプトで呼び出された場合は スクリプトが終了します。また、 設定ファイル auto_prepend_file 又は auto_append_fileオプションで指定されたスクリプトの場合も 同様にそのスクリプトが終了します。

さらに詳しい情報に関しては 返り値を参照してください。

注意: return は関数ではなく言語構造であるため、 引数を括弧で囲う必要があるのはないことに注意しましょう。 括弧で囲わずそのままにしておくのが一般的です。 またそのほうが PHP にかかる負荷も低くなります。

注意: パラメータを省略した場合は括弧も省略する必要があります。この場合の返り値は NULL です。 return に括弧をつけてコールしながら何も引数を指定しないと、 パースエラーとなります。

注意: 変数をリファレンスで返す場合は、決して引数を 括弧で囲ってはいけません。そのようにすると正しく動作しません。 return ($a); とすると、変数を返すのではなく ($a) を評価した結果を返すことになります (この場合は、もちろん $a の値です)。

add a note add a note

User Contributed Notes 4 notes

up
18
warhog at warhog dot net
9 years ago
for those of you who think that using return in a script is the same as using exit note that: using return just exits the execution of the current script, exit the whole execution.

look at that example:

a.php
<?php
include("b.php");
echo
"a";
?>

b.php
<?php
echo "b";
return;
?>

(executing a.php:) will echo "ba".

whereas (b.php modified):

a.php
<?php
include("b.php");
echo
"a";
?>

b.php
<?php
echo "b";
exit;
?>

(executing a.php:) will echo "b".
up
12
Tom
10 months ago
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";
?>

Where "voodoo.php" may be something like:
<?php
return $parameter1 . " " . $parameter2;
?>

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!
up
5
J.D. Grimes
1 year ago
Note that because PHP processes the file before running it, any functions defined in an included file will still be available, even if the file is not executed.

Example:

a.php
<?php
include 'b.php';

foo();
?>

b.php
<?php
return;

function
foo() {
     echo
'foo';
}
?>

Executing a.php will output "foo".
up
-10
andrew at neonsurge dot com
6 years ago
Response to stoic's message below...

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.

Just something to help clarify what you have stated in your comments.
To Top