Results 1 to 3 of 3

Math Help - "((1 + 2 + $var1) * $var2) / 2 - 5", ends "7"

  1. #1
    Newbie
    Joined
    Jan 2011
    From
    United States
    Posts
    1

    "((1 + 2 + $var1) * $var2) / 2 - 5", ends "7"

    I'm going through a php tutorial right now and this math example below is driving me nuts. The exmaple was "((1 + 2 + $var1) * $var2) / 2 - 5", and all the rest of the code below is just me trying to figure it out.

    The answer ends up being "7", but to me it looks like PHP Math is sayding "24 / -3 = 7" when it should be "24 / -3 = -8". What am I missing in this PHP Math issue?? Thanks!

    Code:
    <?php
    $var1 = 3;
    $var2 = 4;
    ?>
        $var1 = 3;<br />
            $var2 = 4;<br /><br />
           <br />
           <h1> ((1  + 2 + $var1) * $var2) / 2 - 5 = <?php echo((1  + 2 + $var1) * $var2) / 2 - 5; ?><br /> </h1>
        <hr />
        <br />
        <br />
        LEFT SIDE:<br />
        (1  + 2 + $var1) * $var2 =  <?php echo ((1  + 2 + $var1) * $var2); ?><br />
    <br />
    RIGHT SIDE:<br />
    2 - 5 =   <?php echo 2 - 5; ?><br />
        <br />
        24 / -3 SHOULD BE:
        <h1>24 / -3 = <?php echo 24 / -3; ?><br /></h1>
    That spits out:

    $var1 = 3;
    $var2 = 4;

    ((1 + 2 + $var1) * $var2) / 2 - 5 = 7

    LEFT SIDE:
    (1 + 2 + $var1) * $var2 = 24

    RIGHT SIDE:
    2 - 5 = -3

    24 / -3 SHOULD BE:
    24 / -3 = -8
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Newbie
    Joined
    Jan 2011
    Posts
    7

    If you need answer -8

    If you need answer -8, you forgot ( )
    it have to look :: "((1 + 2 + $var1) * $var2) / (2 - 5)", ends "7"
    without (2-5) it looks like at first divided to 2 and after that minus 5
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by simy202 View Post
    I'm going through a php tutorial right now and this math example below is driving me nuts. The exmaple was "((1 + 2 + $var1) * $var2) / 2 - 5", and all the rest of the code below is just me trying to figure it out.
    ((1 + 2 + $var1) * $var2) / 2 - 5 evaluates as:

    ((1 + 2 + 3) * 4) / 2 - 5 the innermost bracket is evaluated first to give (6*4)/2-5, now the multiplications and division take precedence so this becomes 12-5 which is 7.

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 3
    Last Post: October 17th 2011, 02:50 PM
  2. Replies: 2
    Last Post: June 4th 2011, 12:11 PM
  3. Replies: 2
    Last Post: April 24th 2011, 07:01 AM
  4. Replies: 1
    Last Post: October 25th 2010, 04:45 AM
  5. Replies: 1
    Last Post: June 4th 2010, 10:26 PM

Search Tags


/mathhelpforum @mathhelpforum