php语法错误,当我试图在函数内部添加foreach时


php syntax error while am trying to add foreach inside function

我正在尝试使用foreach函数到其他函数中写入文件

这是foreach函数

foreach ($from as $ex) { 
                echo "$ex->id ;";
                echo "$ex->nickname ;";
                echo "$ex->date ;";
                echo "$ex->content ;";
                echo "'n";
                }

这是codeigniter写文件函数,我想把我的foreach添加到最后一个参数中

write_file('reports/report-' . date('h-i-s-d-m-y') . '.csv',$i_want_to_put_my_foreach_here);

不能像那样将foreach循环作为函数参数。你需要把它们分开。在这种情况下,您可能想要使用一个临时变量:

foreach ($from as $ex) { 
    $string = "$ex->id ;";
    $string .=  "$ex->nickname ;";
    $string .=  "$ex->date ;";
    $string .=  "$ex->content ;";
    $string .=  "'n";
    write_file('reports/report-' . date('h-i-s-d-m-y') . '.csv', $string);
}