Function return and foreach trouble
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 = 开发者_JS百科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?
The foreach cannot be assigned like this :
$output .= foreach($data->do($two) as $e) {;
$output .= $e->info;
$output .= } ;
Instead, what you want is to loop with the foreach()
, and, inside the loop, assign the current value to your $output
:
foreach ($data->do($two) as $e) {
$output .= $e->info;
}
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.
精彩评论