Page 1 of 1
simple math
Posted: Wed Nov 03, 2004 7:28 pm
by Daisy Cutter
I want to make a loop that will take $x and add one for $n times, printing the result for each one, and using the previous result for the next.
so if $n=4 and $x=1
2
3
4
5
how in the world could i do this?
Posted: Wed Nov 03, 2004 7:59 pm
by markl999
There's a few ways, you could use a for() loop for example, or here's another way.
Code: Select all
$x=1;
$n=4;
echo join('<br />', range($x+1, $x+$n));
Posted: Wed Nov 03, 2004 8:45 pm
by rehfeld
Code: Select all
// for loop version
$x = 1;
$n = 4;
for ($i = 0; $i < $n; $i++) {
$x += 1; // same as $x = $x + 1;
echo "$x<br />";
}
Posted: Wed Nov 03, 2004 8:53 pm
by Daisy Cutter
thanks!
i used the for loop because it suits my needs better. i need to make a solver for a particularly difficult precalculus problem. we can use calculator... I will make my own

i have to do out 2 by hand though. Jeez is it hard.
Posted: Wed Nov 03, 2004 8:59 pm
by rehfeld
this is more simple, but maybe its all you need :
Code: Select all
for ($i = 2; $i <= 5; $i++) {
echo $i;
}