是否可以在字符串串联中使用foreach
?
如下所示:
return '<td>'.
'<div class="form-group">'.
'<select class="form-control">'.
/*This*/ foreach($values as $v){return .'<option>'.$v.'</option>'.;}
'</div>'.
'</div>'.
'</td>';
我想一起做这些,甚至不创建一个变量。
否。但你可以更聪明:
...
implode("",array_map(function($v) {return "<option>".$v."</option>"},$values)).
...
您可以先创建字符串,然后将其添加到最终字符串中。
$text = "";
foreach($values as $v)
{
$text .= '<option>'.$v.'</option>';
}
return '<td>'.
'<div class="form-group">'.
'<select class="form-control">'.
$text .
'</div>'.
'</div>'.
'</td>';