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 7 of 7

Thread: Simple math

  1. #1
    New Coder
    Join Date
    Apr 2009
    Location
    Newport Beach, California
    Posts
    96
    Thanks
    11
    Thanked 1 Time in 1 Post

    Simple math

    When I run this script, I get '7' outputted. How can this be?

    PHP Code:
    <?php
           $var1 
    3;
           
    $var2 4;    
           echo ((
    $var1) * $var2) / 2-5;            
    ?>
    My own math calcs:
    (1+2+3) * 4 / -3 =
    6 * 4 / -3 =
    24 / -3
    -8

    What am I missing?
    Last edited by PonchoX; 02-07-2010 at 04:21 PM.

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    PHP Code:
     2-5
    Division has a priority over subtraction.


    ------------
    Leonard Whistler

  • #3
    New Coder
    Join Date
    Apr 2009
    Location
    Newport Beach, California
    Posts
    96
    Thanks
    11
    Thanked 1 Time in 1 Post
    What does that mean, calc-wise?

    How are you getting 7?

  • #4
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    ( ) brackets first.

    • 1+2+3 = 6
    • 6 * 4 = 24
    • 24 / 2 = 12
    • 12-5 = 7


    -----
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    PonchoX (02-07-2010)

  • #5
    New Coder
    Join Date
    Apr 2009
    Location
    Newport Beach, California
    Posts
    96
    Thanks
    11
    Thanked 1 Time in 1 Post
    Oh,

    I was doing (2-5).

    Thanks.

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    It might be worth you looking at the BODMAS article on wikipedia
    http://en.wikipedia.org/wiki/BODMAS
    this is the order that mathematics is calculated
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #7
    New Coder
    Join Date
    Apr 2009
    Location
    Newport Beach, California
    Posts
    96
    Thanks
    11
    Thanked 1 Time in 1 Post
    Thanks. Good info there.


  •  

    Posting Permissions

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