函数返回和foreach麻烦
我有一个简单的函数:
function name() {
extract( myfunction_atts( array(
'one' => '',
'two' => '',
), $atts ) );
/* CODE */
return $output; /* return dataName(); in second case */
}
现在,我想要返回输出此代码:
$output .= include_once(ADDRESS_CONST."/script.php");
$output .= $data = data($one);
$output .= foreach($data->do($two) as $e) {;
$output .= $e->info;
$output .= } ;
给出语法错误,意外的T_FOREACH。
所以我需要一个函数,要点是:
function dataName() {
/* global $one;
global $two;
doesn't work */
include_once(ADDRESS_CONST."/script.php");
$data = data($one);
foreach($data->do($two) as $e) {;
$e->info;
} ;
}
不“看到”$one 和 $two 变量。我确信我错过了一些东西,可能还有更简单的方法?
I have a simple function:
function name() {
extract( myfunction_atts( array(
'one' => '',
'two' => '',
), $atts ) );
/* CODE */
return $output; /* return dataName(); in second case */
}
Now, I want the return to output this code:
$output .= include_once(ADDRESS_CONST."/script.php");
$output .= $data = data($one);
$output .= foreach($data->do($two) as $e) {;
$output .= $e->info;
$output .= } ;
Gives syntax error, unexpected T_FOREACH.
So I need a function, the point is:
function dataName() {
/* global $one;
global $two;
doesn't work */
include_once(ADDRESS_CONST."/script.php");
$data = data($one);
foreach($data->do($two) as $e) {;
$e->info;
} ;
}
Doesn't "see" $one and $two variables. I'm sure I'm missing something and there's probably an easier way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
foreach 不能这样分配:
相反,您想要的是使用
foreach()
进行循环,并在循环内将当前值分配给您的$output
:基本上来说,
foreach()
是一个控制结构:它允许你的脚本循环;但仅此而已:它本身不返回任何值。另一方面,在循环内部,您可以做任何您想做的事情:
foreach()
仅确保针对数组的每个项目执行此代码。The foreach cannot be assigned like this :
Instead, what you want is to loop with the
foreach()
, and, inside the loop, assign the current value to your$output
:Basically speaking,
foreach()
is a control structure : it allows your script to loop ; but that's all : it doesn't return any value by itself.On the other hand, inside the loop, you can do pertty much whatever you want : the
foreach()
only make sure that this code is executed for each item of your array.