Php/mysql - Incrementing Variable Name To Loop For Data
Solution 1:
You can do this with complex expressions (curly brackets {}) around a variable name.
if(empty(${"player$i"})) {
//player$i is empty
}
complex expressions allow you to set variable names dynamically.
To help you better understand how these work, I will show you that you can also use these just like regular string concatenation like so
$variable = "many test";
echo"this is a test echo. {$variable}";
I commonly use this for generating a variable for many array variables based on their key
$array = array("key1" => "value1", "key2" => "value2");
foreach($arrayas$key => $value) {
${$key} = $value;
}
The code above would create 2 variables, $key1 and $key2, with the appropriate value associated with them.
Alternatively, I'm pretty sure you can just add another $ to the front of your variable, but I would say this is harder to read and figure out what's going on.
$playercheck = "player"+$i;
if($$playercheck != 0) {
$playerspicked++;
}
Solution 2:
In your case there is a much easier way to count all the not null players in the team.
echo count(array_filter($yourTeam));
the array_filter function without a second parameter will automatically remove the null entries.
Post a Comment for "Php/mysql - Incrementing Variable Name To Loop For Data"