Php 内部带有变量的字符串用作循环中的变量
在项目中,我定义了一个文本块,例如:Php 内部带有变量的字符串用作循环中的变量,php,loops,variables,Php,Loops,Variables,在项目中,我定义了一个文本块,例如: $test = '<div>Name'.$i.': <input type="text" name="name'.$i.'" value="'.$_POST['name'.$i].'" /></div>'; $test='Name'.$i.:'; 然后在for循环中使用文本块,如: $_POST['name1'] = 'Max'; for($i=0;$i<3;$i++){ $test =
$test = '<div>Name'.$i.': <input type="text" name="name'.$i.'" value="'.$_POST['name'.$i].'" /></div>';
$test='Name'.$i.:';
然后在for循环中使用文本块,如:
$_POST['name1'] = 'Max';
for($i=0;$i<3;$i++){
$test = '<div>Name'.$i.': <input type="text" name="name'.$i.'" value="'.$_POST['name'.$i].'" /></div>';
echo $test;
}
$\u POST['name1']='Max';
对于($i=0;$i如果您的$string
内容类似于HTML模板,无法在运行中生成,我将使用某种占位符将其保存在数据库中,稍后您将在for循环中替换这些占位符:
'Name%i%:'
那你呢
$string = $thatDbStoredTemplate;
for($i=0;$i<3;$i++){
$test = str_replace(array("%i%", "%name%"), array($i, $_POST["name". $i]), $string);
echo $test;
}
$string=$thatDbStoredTemplate;
对于($i=0;$i必须使用实际代码进行测试(比示例中的代码复杂得多!)-工作起来很有魅力。一如既往,我一直在考虑比必要时更复杂的问题…非常感谢,Jan!!!现在我必须确保索引设置正确-如果我有时间,我会尝试一下你的方法。。。
$_POST['name1'] = 'Max';
$string = '<div>Name'.$i.': <input type="text" name="name'.$i.'" value="'.$_POST['name'.$i].'" /></div>';
for($i=0;$i<3;$i++){
$test = $string;
echo $test;
}
$string = $thatDbStoredTemplate;
for($i=0;$i<3;$i++){
$test = str_replace(array("%i%", "%name%"), array($i, $_POST["name". $i]), $string);
echo $test;
}