Reputation: 447
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 .":";
}
}
Upvotes: 0
Views: 147
Reputation: 2470
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 .":";
}
}
Upvotes: 1