simple math

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

simple math

Post 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?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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));
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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 />";
}
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

Post 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 :D i have to do out 2 by hand though. Jeez is it hard.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

this is more simple, but maybe its all you need :

Code: Select all

for ($i = 2; $i <= 5; $i++) {
    echo $i;
}
Post Reply