PHP函数中的include/require
在PHP函数中包含的文件中是否可能有返回语句 我希望这样做,因为我在单独的文件中有很多函数,它们的顶部都有一大块共享代码PHP函数中的include/require,php,return,break,Php,Return,Break,在PHP函数中包含的文件中是否可能有返回语句 我希望这样做,因为我在单独的文件中有很多函数,它们的顶部都有一大块共享代码 As in function sync() { include_once file.php; echo "Test"; } file.php: ... return "Something"; 当返回的内容出现一次从include_中断而不是sync函数中断时,是否可能中断包含文件的返回 对不起,我的问题有点过分,希望我能解释清楚 谢谢,返回将不起作用,但是如果您试
As in
function sync() {
include_once file.php;
echo "Test";
}
file.php:
...
return "Something";
当返回的内容出现一次从include_中断而不是sync函数中断时,是否可能中断包含文件的返回
对不起,我的问题有点过分,希望我能解释清楚
谢谢,返回将不起作用,但是如果您试图回显include文件中的某些内容并将其返回到其他地方,则可以使用输出缓冲区
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;
}
您可以通过
return
语句将包含文件中的数据返回到调用文件中
include.php
return array("code" => "007", "name => "James Bond");
$result = include_once "include.php";
var_dump("result);
return true;
?>
function foo() {
return (include 'bar.php');
}
print_r(foo());
echo "I will call the police";
return array('WAWAWA', 'BABABA');
file.php
return array("code" => "007", "name => "James Bond");
$result = include_once "include.php";
var_dump("result);
return true;
?>
function foo() {
return (include 'bar.php');
}
print_r(foo());
echo "I will call the police";
return array('WAWAWA', 'BABABA');
但是你不能调用返回$something代码>并将其作为调用脚本中的返回语句<代码>返回
仅在当前范围内有效
编辑:
return true;
?>
<?php
throw new exception();
?>
我希望这样做,因为我有很多
在单独的文件和
他们都有大量的共享资源
代码在顶部
As in
function sync() {
include_once file.php;
echo "Test";
}
在这种情况下,为什么不将这个“共享代码”放在单独的函数中呢?这将很好地完成这项工作,因为拥有函数的目的之一是在不同的地方重用代码,而无需再次编写。我认为它不是这样工作的。include不仅仅是将代码放在适当的位置,它还对代码进行求值。因此,返回意味着您的“include”函数调用将返回该值
return true;
?>
另请参见手册中有关此方面的部分:
处理退货:可以
在
包括文件以便终止
在该文件中处理并返回到
那个叫它的剧本
return语句返回包含的文件,并且不插入“return”语句
有一个示例(示例#5)显示了“return”的作用:
简化示例:
return.php
<?php
$var = 'PHP';
return $var;
?>
testreturns.php
<?php
$foo = include 'return.php';
echo $foo; // prints 'PHP'
?>
testreturns.php
我认为您希望return
的行为更像一个异常,而不是一个return语句。以以下代码为例:
return.php:
return true;
?>
exception.php:
return true;
?>
<?php
throw new exception();
?>
执行以下代码时:
<?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) {}
?>
…因此,您将获得以下输出:
正在执行testReturn()…testReturn()正常执行
正在执行testException()
如果您确实使用异常方法,请确保将函数调用放在一个
try…catch
块中-异常四处传播对业务不利。摇滚乐如下:
return (include 'bar.php');
index.php
return array("code" => "007", "name => "James Bond");
$result = include_once "include.php";
var_dump("result);
return true;
?>
function foo() {
return (include 'bar.php');
}
print_r(foo());
echo "I will call the police";
return array('WAWAWA', 'BABABA');
bar.php
return array("code" => "007", "name => "James Bond");
$result = include_once "include.php";
var_dump("result);
return true;
?>
function foo() {
return (include 'bar.php');
}
print_r(foo());
echo "I will call the police";
return array('WAWAWA', 'BABABA');
输出
return true;
?>
I will call the police
Array
(
[0] => WAWAWA
[1] => BABABA
)
告诉我怎么做就好了
return true;
?>
像这样:
return (include 'bar.php');
祝你有愉快的一天 我不认为这是被问到的。问题似乎是“我可以在包含的文件中放置一个返回,就像在我的函数中调用了‘return’一样。事实并非如此。@Nanne:是的……在发布信息后立即编辑我的答案这是一个朋友“必须”将其所有项目放在令人讨厌的单独文件中的工作:-(@Pez-Cuckow:OK--您可以试试这个(但只有当包含的文件始终使用
return
语句时,它才会起作用):return include\u once“include.php"
。除此之外--建议您的朋友重构代码以使用函数--更好的逻辑和更少的麻烦。请注意,如果第二次访问数组,include_once或require_once将返回true而不是数组。例如,在使用此策略加载配置文件时,这可能是不好的。除非也许有人可以解释-1,这是一个解决问题的可行方法我自己没有-1,但我可以解释:“返回将不起作用”答案的开头是完全错误的。来自包含文件的全局范围的return
将返回一个值,该值用作include语句本身的返回值。