Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.