PHP使用不同的名称动态创建单选按钮
I'm trying to create a page that will dynamically create input buttons if the string contains an "a" and otherwise increment the value and display the string.
Here is my function that checks it, it works but every input has the same name, and $num
does not get updated.
Any help would be much appreciated, thanks in advance.
$num = 0;
function isAnswer($a, $i) {
if (strpos($a, 'a') !== false) {
return '<input type="radio" name="gender'.$num.'" value="male">';
} else {
++$num;
return $a .":";
}
}
我正在尝试创建一个页面,如果字符串包含“a”,则会动态创建输入按钮,否则 递增值并显示字符串。
这是我检查它的函数,它可以工作,但每个输入都有相同的名称, 任何帮助都会非常感谢,提前感谢。 p>
$ num code>不会更新。 p>
$ num = 0;
function isAnswer($ a,$ i){
if( strpos($ a,'a')!== false){
return'&lt; input type =“radio”name =“gender'。$ num。'”value =“male”&gt;';
} else {
++ $ num;
返回$ a。“:”;
}
}
code> pre>
div>
Thats because $num
is outside function scope.
Adding global $num
within function. (quick solution)
Do this:
$num = 0;
function isAnswer($a, $i) {
global $num;
if (strpos($a, 'a') !== false) {
return '<input type="radio" name="gender'.$num.'" value="male">';
} else {
++$num;
return $a .":";
}
}