while loop division go below decided number - php

This code will divide the score until it reaches the number 5.
The $rows[score] is equal to 6600 in the database.
<?php
$i = $rows[score]; //score is 6600 in the database
while ($i >= 5) {
echo $i = $i /2;
echo "<br>";
}
?>
This is what my browser outputs:
3300
1650
825
412.5
206.25
103.125
51.5625
25.78125
12.890625
6.4453125
3.22265625
I don't understand why the browser output the last 3.22 - how do I stop the loop from echo out the last one that is less than 5??

Nothing wrong here the last value you get is from 6.4453125 / 2 = 3.22265625 since 6.4453125 still greater than 5

because 6 is higher than 5? so it does one more loop making $i 3.2 where the loop stops

If ($i<5) it wont go into the loop but there is no way to know until you check. $i = 6.4453125 the last time it checks, so it goes into the loop and it divides it by 2, which makes it less than 5 so it doesn't go into the loop again and stops.

I found the way to answer my own question so 3,22 will not be viewed on the page.
<?php
$i = $rows[score]; //score is 6600 in the database
while ($i >= 5) {
$i = $i /2;
if($i >= 5) {
echo $i;
echo "<br>";
}
}
?>

Since you're dividing by two immediately before displaying the result, you want to stop your loop when $i >= (5*2) i.e. $i >= 10, not 5.
<?php
$i = $rows[score]; //score is 6600 in the database
while ($i >= 10) {
echo $i = $i /2;
echo "<br>";
}
?>
This gives:
3300
1650
825
412.5
206.25
103.125
51.5625
25.78125
12.890625
6.4453125

Related

(PHP) Why does this generator-script adds the starting value + the step-value to the starting value?

the title may be pretty confusing. Here's a script which requires a start-end and step value to add numbers to the start value:
function addieren($start, $ende, $schritt = 1) {
if ($start < $ende) {
$erg = 0;
for ($i = $start; $i <= $ende; $i += $schritt) {
$erg += $i;
yield $erg;
}
}
}
foreach (addieren(2, 10, 2) as $erg) {
echo $erg . "<br>";
}
So the start value is 2, the end value is 10 (but it's not ending at 10 as the result shows), and it should add 2 to the $i every step.
Here's the output:
2
6
12
20
30
The first output is clear to me, since the $erg was 0 and it added the 2 because of 2 steps.
But the next output is 6 and I don't get why. In the second loop, $i is 2 and and the script says: $i += $schritt so when $i is 2 and the $schritt value is also 2, why doesn't that output 4 as the second output? Hope you get what I mean.. I guess it's a pure logic error in my head.

PHP: How to loop from 100 to 0 by -2 without letting it go infinitely?

what I am trying to achieve here is to be able to loop from 0 to 100 (100, 98, 96, 94 ...) but has to stop at 0. What is doing right now is it passes 0 and -2 -4 which crashes the server. What am I doing wrong?
for ($i = 100; $i <= 100; $i--){
echo $i--;
echo "<br>";
}
Maybe a little explanation would be useful.
The middle part of the for loop $i <= 100 is what makes it infinite. That expression is checked before each iteration of the loop, and the loop will continue as long as that expression evaluates to true.
Since you set $ito 100 in the first section of the loop, and you're doing nothing except making it smaller, it will always be <= 100, forever.
The loop will work fine just the way you have it written if you change the continuation condition.
for ($i = 100; $i >= 0; $i--){
echo $i--;
echo "<br>";
}
That way it will continue until $i is reduced to less than zero, then $i >= 0 will be false, and the loop will end.
The third argument in for loop is what will be executed at the end of the loop. So:
for ($i = 100; $i >= 0; $i -= 2){
echo "$i<br>";
}
Will do the trick
As you can read here https://secure.php.net/manual/en/control-structures.for.php
At the end of each iteration, expr3 is evaluated (executed).
Alternatively:
<?php
foreach(range(100, 0, -2) as $n) {
echo $n;
}
for ($i = 100; $i >= 0; $i-=1){
echo $i--;
echo "<br>";
}
I figured it out somehow. Has been studying JavaScript for a year; Loop is still confusing to me

Is my second for loop able to iterate using the increment from my first loop?

What I want is the first loop iterating from 1 to 4 and the second loop from 5 to 6.
Here is my code:
<?php
for ($i = 1 ; $i <= 4 ; $i++)
{
echo $i . "<br>";
}
?>
<hr>
<?php
for ($i = 1 ; $i <= 2 ; $i++)
{
echo $i . "<br>";
}
?>
The loops you've given are:
1st loop: from 1 to 4
2nd loop: from 1 to 2
First loop is ok, but seconds needs to be modified. Use $i<=6 and don't initialize $i variable.
This will give you:
1st loop: from 1 to 4
2nd loop: from (value that 1st loop have ended)+1 to 6, so (4+1) to 6, 5 to 6
<?php
$i = 0; // be sure 'i' is visible in both loops
for ($i=1; $i<=4; $i++) // form 1 to 4
{
echo $i . "<br>";
}
?>
<hr>
<?php
$i++; // start from 5, not 4
for (; $i<=6; $i++) // from the previous value to 6
{
echo $i . "<br>";
}
?>
Your problem
The second for loop resets your $i variable to 1:
for ($i = 1 ; $i <= 2 ; $i++)
Solution
You can use a while loop instead of your second for loop:
<?php
for ($i = 1; $i <= 4; $i++)
{
echo $i . "<br>";
}
?>
<hr>
<?php
while ($i <= 6) // `<= 6` instead of `<= 2`, since we keep $1's value
{
echo $i . "<br>";
$i++;
}
?>
Rather than using two loops for this, why not just output the <hr> tag at the appropriate point within the same one? If you carry on with adding extra loops, first of all you'll run into confusing problems like this about (re-)initialising variables, and you'll also quickly end up with a lot of unnecessary duplicated code.
You can use the PHP modulo operator (%) to output the <hr> tag after every fourth element, which will both reduce the complexity and be a lot more extensible if you later add more elements:
for ($i=1; $i<=6; $i++) {
echo $i . "<br>";
if ($i % 4 === 0) {
echo "<hr>";
}
}
See https://eval.in/976102

while loop with number of days in a month

I'm trying to get this piece of script to work, but it keeps dying
$currentdays = intval(date("t"));
echo $currentdays; //echoes 30 as we're in April
$i = 1;
while ($i <= $currentdays){
echo $day;
}
It keeps dying with no error. I feel like it's timing out, but it's certainly taking its time.
You need to increment $i. 1 will always be less than 30, creating an infinite loop.
$currentdays = intval(date("t"));
$i = 0;
while ($i++ < $currentdays){
echo $i; // outputs 1, 2, 3.. 30
}
$i never changes. Try this:
$currentdays = intval(date("t"));
echo $currentdays; //echoes 30 as we're in April
$i = 1;
while ($i++ < $currentdays){
echo $i;
}
You never increment $i after echoing $day. This will go into an infinite loop.

how to do this.. backwards?

I currently have:
$i = 1;
while {
echo $i;
$i++;
}
And it shows:
1
2
3
4 etc..
How would I make it display backwards?
For example
4
3
2
1 etc..
I basically want to do the exact same thing but flip it around.
$i = 10;
while($i>0) {
echo $i;
$i--;
}
Example - Print number through 0 to 5 with PHP For Loop
for($i=0; $i<=5; $i=$i+1)
{
echo $i." ";
}
In the above example, we set a counter variable $i to 0. In the second statement of our for loop, we set the condition value to our counter variable $i to 5, i.e. the loop will execute until $i reaches 5. In the third statement, we set $i to increment by 1.
The above code will output numbers through 0 to 5 as 0 1 2 3 4 5.
Note: The third increment statement can be set to increment by any number. In our above example, we can set $i to increment by 2, i.e., $i=$i+2. In this case the code will produce 0 2 4.
Example - Print number through 5 to 0 with PHP For Loop
What if we want to go backwards, that is, print number though 0 to 5 in reverse order? We simple initialize the counter variable $i to 5, set its condition to 0 and decrement $i by 1.
for($i=5; $i>=0; $i=$i-1)
{
echo $i." ";
}
The above code will output number from 5 to 0 as 5 4 3 2 1 0 looping backwards.
Good luck! :)
If you want to back-word sr number as per your count of rows in result then use this.
$num_rows = mysqli_num_rows($query);
$x = $num_rows;
$x--;
$i = 4;
while($i > 0) {
echo $i--;
}

Categories