在函数中使用PHP include/require

8

在PHP中,函数内部的包含文件可以包含返回语句吗?

我想这么做是因为我有很多单独的文件中的函数,在它们的顶部都有大量共享的代码。

As in
function sync() {
  include_once file.php;
  echo "Test";
}

file.php:

...
return "Something";

目前看起来,return语句似乎会跳出include_once函数而不是同步函数,那么被包含文件的return语句是否有可能跳出呢?

抱歉问题表达得有些奇怪,希望我能让它变得更加清晰易懂。

谢谢!

5个回答

7
你可以使用 return 语句将包含文件中的数据返回给调用文件。 include.php
return array("code" => "007", "name => "James Bond");

file.php

$result = include_once "include.php";
var_dump("result);

但是你不能调用return $something;并将其作为调用脚本中的返回语句。 return仅在当前范围内起作用。 编辑:

我想这样做,因为我有很多函数在不同的文件中,并且它们都有一个大块共享代码。

在这种情况下,为什么不将这个“共享代码”放入单独的函数中--这将很好地完成工作,因为函数的一个目的是在不编写重复代码的情况下在不同的位置重用代码。

我不认为这是被问到的内容。问题似乎是“我能在包含的文件中放置一个return语句,它会像在我的函数中调用'return'一样起作用吗?”但事实并非如此。 - Nanne
@Nanne:是的,我发帖后立即开始编辑我的回答。 - LazyOne
不幸的是,这是为一个朋友工作,他“必须”将所有项目放在烦人的单独文件中 :-( - Pez Cuckow
@Pez Cuckow:好的——你可以尝试这个(但只有在包含文件始终使用return语句时才有效):return include_once "include.php";。除此之外,建议你的朋友重构代码以使用函数——逻辑更好,麻烦更少。 - LazyOne
请注意,使用 include_once 或 require_once 在第二次访问时将返回 true 而不是数组。例如,在使用此策略加载配置文件时,这可能会导致问题。除非您确定它只能触发一次,否则请改用 include 或 require。 - codercake

2

使用return语句无效,但如果您想在包含文件中输出一些内容并将其返回到其他地方,则可以使用输出缓冲区。

function sync() {
  ob_start();
  include "file.php";
  $output = ob_get_clean();
// now what ever you echoed in the file.php is inside the output variable
  return $output;
}

也许有人可以解释一下为什么要用-1,这是修复问题的可行方法。 - Ibu
我没有将自己减1,但我可以解释:答案开头的“return不起作用”是完全错误的。从一个包含文件的全局范围中返回将返回一个值,该值用作包含语句本身的返回值。 - FrancescoMM

2
我认为它不是这样工作的。包含操作不仅仅是把代码放在指定位置,还要对其进行评估。因此,return 表示您的 'include' 函数调用将返回该值。
请参阅手册中有关此内容的部分:
处理返回:可以在包含的文件中执行 return() 语句,以终止在该文件中的处理并返回调用它的脚本。
return 语句返回所包含的文件,并不插入 "return" 语句。 manual 中有一个示例(示例#5)展示了 'return' 的作用:
简化示例:

return.php

<?php  
$var = 'PHP';
return $var;
?>

testreturns.php

<?php   
$foo = include 'return.php';
echo $foo; // prints 'PHP'
?>

0

像这样摇滚:

index.php

function foo() {
   return (include 'bar.php');
}
print_r(foo());

bar.php

echo "I will call the police";
return array('WAWAWA', 'BABABA');

输出

I will call the police
Array
(
    [0] => WAWAWA
    [1] => BABABA
)

只是展示给我如何操作

像这样:

return (include 'bar.php');

祝你有美好的一天!


0

我认为你期望return的行为更像异常而不是返回语句。以以下代码为例:

return.php:

return true;

?>

exception.php:

<?php

throw new exception();

?>

When you execute the following code:

<?php

function testReturn() {
    echo 'Executing testReturn()...';
    include_once('return.php');
    echo 'testReturn() executed normally.';
}

function testException() {
    echo 'Executing testException()...';
    include_once('exception.php');
    echo 'testException() executed normally.';
}

testReturn();

echo "\n\n";

try {
    testException();
}
catch (exception $e) {}

?>

...you get the following output as a result:

Executing testReturn()...testReturn() executed normally.

Executing testException()...

If you do use the exception method, make sure to put your function call in a try...catch block - having exceptions flying all over the place is bad for business.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接