Mastering PHP Math: Solving a Tricky Example in a Tutorial

  • Thread starter Thread starter simy202
  • Start date Start date
  • Tags Tags
    Php
AI Thread Summary
The PHP math example presented involves the expression "((1 + 2 + $var1) * $var2) / 2 - 5", which simplifies to 7 when $var1 is 3 and $var2 is 4. The confusion arises from the order of operations, where the division is performed before the subtraction, leading to a misunderstanding of the result. The calculation shows that the left side equals 24, while the right side evaluates to -3, resulting in 24 divided by -3 equaling -8, not 7. To achieve the expected output, the expression should be adjusted to "((1 + 2 + $var1) * $var2) / (2 - 5)". Understanding operator precedence in PHP is crucial for resolving such issues.
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); ?>
 
Insights auto threads is broken atm, so I'm manually creating these for new Insight articles. In Dirac’s Principles of Quantum Mechanics published in 1930 he introduced a “convenient notation” he referred to as a “delta function” which he treated as a continuum analog to the discrete Kronecker delta. The Kronecker delta is simply the indexed components of the identity operator in matrix algebra Source: https://www.physicsforums.com/insights/what-exactly-is-diracs-delta-function/ by...
Fermat's Last Theorem has long been one of the most famous mathematical problems, and is now one of the most famous theorems. It simply states that the equation $$ a^n+b^n=c^n $$ has no solutions with positive integers if ##n>2.## It was named after Pierre de Fermat (1607-1665). The problem itself stems from the book Arithmetica by Diophantus of Alexandria. It gained popularity because Fermat noted in his copy "Cubum autem in duos cubos, aut quadratoquadratum in duos quadratoquadratos, et...
Thread 'Imaginary Pythagorus'
I posted this in the Lame Math thread, but it's got me thinking. Is there any validity to this? Or is it really just a mathematical trick? Naively, I see that i2 + plus 12 does equal zero2. But does this have a meaning? I know one can treat the imaginary number line as just another axis like the reals, but does that mean this does represent a triangle in the complex plane with a hypotenuse of length zero? Ibix offered a rendering of the diagram using what I assume is matrix* notation...

Similar threads

Replies
21
Views
6K
Replies
3
Views
1K
Replies
13
Views
2K
Replies
3
Views
9K
Replies
59
Views
2K
Replies
24
Views
2K
Replies
55
Views
5K
Back
Top