...

View Full Version : subtracting from a number untill its equal to another number..



sir pannels
02-06-2003, 11:52 AM
hey..
hmm ok as the title says.. ive searched on the net for any code on this.
i know i need to use a forloop but im unsure how, i never liked loops lol.
i have a val you lets call $val1 and another $val2 and $val3
and i need to do some kinda lop so that it keeps subtracting.. hmm lets say 50 from val1 untill it is equal to or less than $val2and once it is it gives you the final result as $val3.
any ideas?
thanks:D
P:cool:

jcma
02-06-2003, 12:28 PM
$var1 = X;
$var2 = Y;
$var3 = $var1;
while($var3 > $var2)
$var3 -=50;

echo "var1 $var1<br>var2 $var2 <br>var3 $var3";

sir pannels
02-06-2003, 02:58 PM
hey.
Thanks -
i put that r ight into a php file an dadded the <? ?> but i get a error on line 4.. any ideas?

jcma
02-06-2003, 03:04 PM
Originally posted by sir pannels
hey.
Thanks -
i put that r ight into a php file an dadded the <? ?> but i get a error on line 4.. any ideas?

try changing:
$var1 = X and $var2=Y to numbers, like $var1 = 1234 and $var2 = 12

sir pannels
02-06-2003, 03:06 PM
i already tried that.... thats what i thought first off but it didnt make a defference.
should this work on all versions of php?

jcma
02-06-2003, 03:10 PM
this works fine for me.




<?php

$var1 = 251;
$var2 = 49;
$var3 = $var1;
while($var3 > $var2)
$var3 -=50;

echo "var1 $var1<br>var2 $var2 <br>var3 $var3";

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum