1

I have rows in my teams table named player1, player2, player3 ... player12. In PHP script i set them as variables ($player1,$player2...) and want to loop through them to check if they are NULL, and if they are not to count them.

How may I increment a variable in PHP? I have tried doing it likes this:

<?
    $playerspicked = 0;
    for($i = 1; $i <= 12; $i++) {
        $playercheck = "$player"+$i;
        if($playercheck != 0) {
            $playerspicked++;
        }
    }
?>

but this wouldn't work.

5
  • 1
    Normalize your data, and then you wouldn't need to use such a dirty solution Commented Aug 31, 2017 at 20:29
  • @MarkBaker what do you mean by normalize? Commented Aug 31, 2017 at 20:48
  • I mean structure your database tables.... having numbered column names is generally an indication that you should be using a separate table with a row for each player Commented Aug 31, 2017 at 20:52
  • A schema like that is a huge violation of the Zero, One or Infinity Rule of database normalization. If you created a proper relational table this would be trivial. I'd strongly recommend redoing your schema before trying to solve this problem: You'll only fossilize this really awkward design. Commented Aug 31, 2017 at 21:15
  • Oh, I probably gave way too little details and got misunderstood. In table named teams there are teams added by users. Player1, 2 etc are just kind of settings, user can pick up to 12 members for his sport team from a predefined list. I'm not storing multiple players data in a single row :-) Commented Aug 31, 2017 at 21:18

2 Answers 2

3

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($array as $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++;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for sharing these methods, they are of much help!
1

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.

1 Comment

That seems really simple and effective as well. If I have some more rows in my $team table, for example two that are not players, all I have to do is decrease the count() by 2 with simple arithmetic?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.