Mastering PHP Math: Solving a Tricky Example in a Tutorial

  • Context: Undergrad 
  • Thread starter Thread starter simy202
  • Start date Start date
  • Tags Tags
    Php
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 2K views
simy202
Messages
1
Reaction score
0
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 is sayding "24 / -3 = 7" when it should be "24 / -3 = -8". What am I missing in this http://www.phpkode.com/scripts/category/php-math/" issue?? Thanks!

PHP:
<?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
 
Last edited by a moderator:
Mathematics news on Phys.org
Try:
<?php echo((1 + 2 + $var1) * $var2) / (2 - 5); ?>