Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts

    subtracting from a number untill its equal to another number..

    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
    P

  • #2
    New Coder
    Join Date
    Jan 2003
    Location
    Lisbon / Portugal
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
       $var1 X;
       
    $var2 Y;
       
    $var3 $var1;
       while(
    $var3 $var2)
            
    $var3 -=50;

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

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    hey.
    Thanks -
    i put that r ight into a php file an dadded the <? ?> but i get a error on line 4.. any ideas?

  • #4
    New Coder
    Join Date
    Jan 2003
    Location
    Lisbon / Portugal
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    i already tried that.... thats what i thought first off but it didnt make a defference.
    should this work on all versions of php?

  • #6
    New Coder
    Join Date
    Jan 2003
    Location
    Lisbon / Portugal
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this works fine for me.

    PHP Code:

    <?php

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

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

    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •