i need to put the last while loop result into a variable. How can i return only the last one?
<?php
$x = 0;
$r = rand(0,5);
$d = 10 + $r;
$h = 143;
$counter = 0;
while ($h >= 0) {
echo "The number is: $h <br>";
$h-=$d;
$counter++;
}
?>
Related
This is the code I have. It currently works as is, However I'm experimenting with loops and want to see it can be done with a while loop and how it would be done. With this code I can take 2 input numbers and display them, then point out all odds, add all evens, and add all the squares of the odds.
define ("B","<br/>");
$firstNum = $_POST["firstNum"];
$secondNum = $_POST["secondNum"];
if ($firstNum < $secondNum)
{
$firstNum = true;
}
elseif ($firstNum >= $secondNum)
{
$firstNum = "You didn't listen, dumb dumb!".'<br/>GO BACK';
}
echo "First Number: ".$firstNum."<br/>"."Second Number: ".$secondNum;
echo B;
echo B;
$numbers = array();
$numbers = range($firstNum, $secondNum);
$length = count($numbers);
$odds = array();
$sumSqOdds = 0;
$sumEven = 0;
$j = 0;
for ($x = 0; $x < $length; $x++)
{
if (($numbers[$x] % 2) == 1)
{
$odds[$j] = $numbers[$x];
$sumSqOdds = $sumSqOdds + pow ($numbers[$x], 2);
$j++;
}
else
{
$sumEven = $sumEven + $numbers[$x];
}
}
$x = 0;
$y = 0;
printf("The odd numbers between your integers are: ");
for ($x = 0; $x < $j; $x++)
{
echo $odds[$x];
echo ' ';
$y++;
if (($y % 10) == 0)
{
echo B;
}
}
echo B;
echo B;
printf("The sum of all even numbers between your integers is: ".$sumEven);
echo B;
echo B;
printf("The sum of the square of the odd numbers between your integers is: ".$sumSqOdds);
Here is my while loop but it seems to be infinite...
$numW = array ();
$numW = range ($firstNum, $secondNum);
$lengthW = count ($numW);
$oddsW = array ();
$sumSqOddsW = 0;
$sumEvenW = 0;
$j = 0;
$x = 0;
while ($x < $lengthW)
{
if (($numW[$x] % 2) == 1)
{
$oddsW[$j] = $numW[$x];
$sumSqOddsW = $sumSqOddsW + pow ($numW[$x], 2);
$x++;
$j++;
}
else
{
$sumEvenW = $sumEvenW + $numW[$x];
}
}
$x = 0;
$y = 0;
printf ("The odd numbers between your integers are: ");
while ($x < $j)
{
$x++;
echo $oddsW[$x];
echo "nbsp;";
$y++;
if (($y % 10) == 0)
{
echo B;
}
}
Equivalent loops:
for ($i = 0; $i < 10; $i++) {
echo $i;
}
$i = 0;
while ($i < 10) {
echo $i;
$i++;
}
For a loop to ever finish it has to change one of the two evaluating variables. So either $x, or $lengthW would have to change during iteration. You made an if statment, in the first case you define that X increases by 1, but in the else case you do not change any variable that then has an effect on either $x, or $lengthW
Nor is there any check that sees if the else state has been reached and to catch that by either changing $x or $lengthW in a later iteration.
As such there's an infinite loop as soon as you reach the else case.
The if statement uses the same $x value as the last iteration checking the same position of the $numW, as such nothing has changed since the last iteration and you'll hit the else again, and again, and so on.
while ($x < $lengthW)
{
if (($numW[$x] % 2) == 1)
{
$oddsW[$j] = $numW[$x];
$sumSqOddsW = $sumSqOddsW + pow ($numW[$x], 2);
$x++; //$x is increased by one, and as such, the loop will progress.
// remove this $x++ if you place it outside the if else statement.
$j++;
}
else // reached when ($numW[$x] %2) != 1
{
$sumEvenW = $sumEvenW + $numW[$x];
// No changes to $x or $lengthW as such you'll hit the else again
// this could be solved by either adding $x++; here.
}
// or by adding $x++; here
// (if you do add it here, remove it in the if case above,
// or you risk increasing it by 2 every iteration
}
I have an array containing a range of numbers 1-100:
$range = range(1, 100);
I want to loop through and assign each a value of 1-24. So 1=1, 2=2, 24=24, but then also 25=1, 26=2, 27=3, etc...
How can I loop through $range and apply said values to each number?
Note: I would preferably like to use a forloop, but will take any valid answer.
The modulo operator (%) is the answer
$range = range(1, 100);
$rangeValues = array();
for ($i = 0; $i < count($range); $i++){
// using modulo 25 returns values from 0-24, but you want 1-25 so I use ($i % 24) +1 instead which gives 1-24
$rangeValues[$range[$i]] = ($i % 24) +1;
}
Try : php modulo operator (%).
//example loop
$range = range(1, 100);
$yourIndex = array();
for ($i = 0; $i < count($range); $i++){
//$yourIndex will reset to 1 after each 25 counts in $range
$yourIndex[$range[$i]] = ($i + 1) % 25;
}
$range = range(1, 100);
$offset = 1;
$limit = 24;
for($i = 0; $i < count($range); $i++)
{
$range[$i] = $offset+($i%$limit);
}
var_dump($range);
For getting your required solution, you can also use the below code -
$range = range(1, 100);
for($i=0; $i<100; $i++){
if($i < 24){
echo $range[$i].' = '.($i+1);echo "<br>";
}else if($i < 48){
echo $range[$i].' = '.($i-23);echo "<br>";
}else if($i < 72){
echo $range[$i].' = '.($i-47);echo "<br>";
}else if($i < 96){
echo $range[$i].' = '.($i-71);echo "<br>";
}else{
echo $range[$i].' = '.($i-95);echo "<br>";
}
}
In a small project I would link to print 1,2,5,10,17,26,37,50,65.This number is increased by an odd number like 1,3,5,7,9,11,13.
I've been unable to find any way to print.
any suggest?
$counter = 0;
$maxCount = 1000;
$sum = 0;
while($counter <=$maxCount)
{
if ($counter % 2 != 0)
{
$sum += $counter;
}
echo $sum . "<br>";
$counter++;
}
You can do this using while() loop,
$start = $interva1 = 1;
$maxCount = 100;
while($start < $maxCount){
echo $start . " ";
$start += $interva1;
$interva1 += 2;
}
$start is the number you want to start the sequence with
$interval is the odd number to be added in each iteration of the loop
This can be done by using a for loop and increment by 2. If you start on an odd number as gap, the gap will always stay an odd number.
$sum = 1;
$maxGap = 1000;
for ($gap = 1; $gap <= $maxGap; $gap += 2) {
echo $sum . "<br />";
$sum += $gap;
}
I have a 6x6 bingo card which is generated with random numbers between 10 and 70. The 7th row and 7th column are used to count the drawn numbers that are on the card. When a row or column reaches 6, there is bingo.
The eventual result I get is right, but in the proces of getting there a few things are going wrong.
In my function generateCard I create the rows and columns of numbers for the card. I think the problem lies in this function.
function generateCard()
{
$card = array();
for ($row = 1; $row < 8; ++$row)
{
$card[$row] = array();
$deck = array(0,1,2,3,4,5,6,7,8,9);
for ($kolom = 1; $kolom < 8; ++$kolom) {
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
$card[$row][] = $row . $number;
unset($deck[$index]);
$deck = array_values($deck);
}
//Test
printCard($card);
// 7th column
$card[$row][7] = 0;
}
//Test
printCard($card);
// 7th row
for ($kolom = 1; $kolom < 7; ++$kolom){
$card[7][$kolom] = 0;
}
//Test
printCard($card);
return $card;
}
I've put in a few printCard functions to test the outcome.
The first test above the creation of the 7th column gives me a ton of undefined offset 7 notices. I figured this happens because the column does not exist, but when I try to create this earlier, I still get the notices.
The second test above the 7th row shows that the 7th row (which should be 0) gets filled with numbers. This gets overwritten after with 0 values. I figured I could fix this by putting $row < 7 and $kolom < 7, but when I do this the card won't get printed right at all.
I'm wondering why i'm getting all these undefined offset notices about the last column (even when I create it earlier) and why I can't use $row < 7 and $column < 7 in both the for loops of generate card to avoid filling up the last row and column (7x7) with values. These should be 0 before the bingo game starts.
Do you have any suggestions?
I think i'm overlooking a few things here..
I will put the complete code here:
mt_srand((double)microtime()*1000000);
function generateCard()
{
$card = array();
for ($row = 1; $row < 8; ++$row)
{
$card[$row] = array();
$deck = array(0,1,2,3,4,5,6,7,8,9);
for ($kolom = 1; $kolom < 8; ++$kolom) {
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
$card[$row][] = $row . $number;
unset($deck[$index]);
$deck = array_values($deck);
}
//Test
printCard($card);
// 7th column
$card[$row][7] = 0;
}
//Test
printCard($card);
// 7th row
for ($kolom = 1; $kolom < 7; ++$kolom){
$card[7][$kolom] = 0;
}
//Test
printCard($card);
return $card;
}
function printCard($card){ ?>
<table border="1" cellspacing="0" cellpadding="5">
<?php for($rij = 1; $rij < 8; $rij++) { ?>
<tr>
<?php for ($kolom = 1; $kolom < 8; $kolom++) { ?>
<td<?php if (($card[$rij][7] == 6) || ($card[7][$kolom] == 6)) { echo ' style="background-color:green"'; } ?>><?php echo $card[$rij][$kolom]; ?></td>
<?php }
} ?>
</tr>
</table>
<?php }
$card = generateCard();
$getrokkenGetallen = array();
$deck = range(10,69);
$bingo = false;
while (!$bingo){
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
if(!in_array($number, $getrokkenGetallen)){
unset($deck[$index]);
$deck = array_values($deck);
$getrokkenGetallen[] = $number;
for ($row = 1; $row < 7; $row++) {
for ($kolom = 1; $kolom < 7; $kolom++) {
if ($card[$row][$kolom] == $number) {
$card[$row][7] += 1;
$card[7][$kolom] += 1;
if(($card[$row][7] == 6) || ($card[7][$kolom] == 6)){
$bingo = true;
}
break;
}
}
}
}
}
echo '<h2>Bingokaart waarop BINGO is gevallen</h2>';
printCard($card);
echo '<p><strong>Getrokken getallen:</strong><br>';
foreach($getrokkenGetallen as $value)
{
echo $value . ' ';
}
echo '</p>';
echo '<p><strong>Aantal getallen dat is getrokken:</strong> ';
echo count($getrokkenGetallen);
echo '</p>';
Example of the output:
Thank you in advance for any help or suggestions.
I'm not exactly sure where your error comes from, and I'm not a regular PHP user, but if i change the generateCard function to this:
function generateCard(){
$card = array();
for ($row = 1; $row < 8; ++$row){
$card[$row] = array();
$deck = range($row*10, $row*10+9);
shuffle($deck);
for($col = 1; $col < 8; ++$col){
if($row == 7 OR $col == 7){
$card[$row][$col] = 0;
}else{
$card[$row][$col] = $deck[$col];
}
}
}
//Test
printCard($card);
return $card;
}
It all works as expected. It's a little simpler than the route you were going i think.
$x = 1;
$num = 15;
while($x <= $num) {
$res = '[TR][TD="align: left"]'.$x.'[/TD][TD="align: left"][/TD][/TR]';
$x++;
}
echo $res;
That's my code but it only shows the following when I try it:
[TR][TD="align: left"]15[/TD][TD="align: left"][/TD][/TR]
It should show that but instead of only 15 it should be 1-15 inclusive.
Any ideas?
You keep setting $res to a new value rather than appending to it. Using $res .= 'something'; is like saying $res = $res . 'something';. Doing this will allow you to keep the previous value of $res and appending more to the end of it.
$x = 1;
$num = 15;
$res = '';
while($x <= $num) {
$res .= '[TR][TD="align: left"]'.$x.'[/TD][TD="align: left"][/TD][/TR]';
$x++;
}
echo $res;
$x = 1;
$num = 15;
while($x <= $num) {
$res = '[TR][TD="align: left"]'.$x.'[/TD][TD="align: left"][/TD][/TR]';
$x++;
echo $res;
}
Put echo $res; inside the loop, otherwise it will echo just the $res from the last loop cycle instead of all 15 times.