php - Strpos is not working. I am using it to search number -


i trying build table (6x6) bomb or not in it. using following method randomise 10 bombs in 36 blocks. gives me bomb everytime.

    $i = 1;     $numbers = null;     $j = 0;     while ($j < 10)     {         $sample = rand (1,36);         if (strpos($numbers, $sample) === false)          {             $numbers = $numbers."".$sample.",";             $j++;         }     }      echo $numbers;      while ($i < 37)     {         if (strpos($numbers, $i) !== false)         {             echo '<td><button value="safe" onclick="bombcheck(this.id)"             id="'.$i.'" class="closed"></button></td>';         }         else         {             echo '<td><button value="bomb" onclick="bombcheck(this.id)"             id="'.$i.'" class="closed"></button></td>';         }         if ($i%6 == 0)          {             echo "</tr><tr>";         }         $i++;     } 

you approaching wrong way. since want accumulate 10 numbers, use array hold them. additionally, since want pick 10 out of 36 (which sizable portion of small number) best approach be:

$squares = array_fill_keys(range(1, 36), true); $bombs = array_rand($squares, 10); 

you can check if square $x has bomb with

$hasbomb = in_array($x, $bombs); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -