...

View Full Version : Resolved Simple math



PonchoX
02-07-2010, 09:11 AM
When I run this script, I get '7' outputted. How can this be?


<?php
$var1 = 3;
$var2 = 4;
echo ((1 + 2 + $var1) * $var2) / 2-5;
?>

My own math calcs:
(1+2+3) * 4 / -3 =
6 * 4 / -3 =
24 / -3
-8

What am I missing?

Len Whistler
02-07-2010, 09:14 AM
/ 2-5;

Division has a priority over subtraction.


------------

PonchoX
02-07-2010, 09:15 AM
What does that mean, calc-wise?

How are you getting 7?

Len Whistler
02-07-2010, 09:18 AM
( ) brackets first.


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


-----

PonchoX
02-07-2010, 09:21 AM
Oh,

I was doing (2-5).

Thanks.

JAY6390
02-07-2010, 01:48 PM
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

PonchoX
02-07-2010, 05:11 PM
Thanks. Good info there.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum