Jump to content

Help understanding array and input looping


cloudnyn3

Recommended Posts

Hi there, so I'm having a little trouble understanding looping and arrays or inputs. I'm a Pshell programmer and things are done MUCH differently lol

 

So heres the script I've created.

<?php
$first=400;
$second=300;

for ($i = $first; $second < $i; $i++) {
    echo $i;
}

?>

What I'm trying to accomplish is taking the first Number and the second and get it to count or "loop" up to 400 from the lower number. Then have it output the result. I'm not sure what I'm doing wrong here though......

It counts up from 400 and never ends? The for loop will keep running as long as $second < $i; $second=300 and $i=400, but $i only counts up (the $i++) so it'll never drop below $second.

 

for ($i = $second /* the lower number */; $i <= $first /* keep going while $i doesn't exceed $first */; $i++ /* counts up */) {

Ah thank you, that was my mistake for not paying attention. So when it outputs, its just a long ass string of numbers. like REALLY long. shouldn't it just stop and output the value of the 2nd number? which would in theory be 400 now that its incremented up?

It does what you tell it to do. In this case echo the value of $i each iteration of the loop.

 

It sounds like you want

$first = 400;
$second = 300;
$i = $first;

No long list of numbers and $i ends up with the value of $first

It does what you tell it to do. In this case echo the value of $i each iteration of the loop.

 

It sounds like you want

$first = 400;
$second = 300;
$i = $first;

No long list of numbers and $i ends up with the value of $first

esentially what I'm doing is this 

 

<?php
 
$first=400;
$second=300;
 
echo "$first - $second";
 
?>

 

I'm looking to do it using a loop around?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.