# Mastering PHP Math: Solving a Tricky Example in a Tutorial

• simy202
In summary, the conversation is about a math example in a PHP tutorial that is causing confusion. The code includes variables $var1 and$var2 and an equation that is supposed to result in 7, but instead results in -8. The conversation includes attempts to figure out the issue and a code snippet that produces the incorrect result. The suggested solution is to use parentheses to properly group the equation.

#### simy202

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/" [Broken] 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:
Try:
<?php echo((1 + 2 + $var1) *$var2) / (2 - 5); ?>

## 1. What is the difference between mathematical operators in PHP?

In PHP, there are various mathematical operators such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). The addition operator is used to add two numbers together, the subtraction operator is used to subtract one number from another, the multiplication operator is used to multiply two numbers, the division operator is used to divide one number by another, and the modulus operator is used to find the remainder after division.

## 2. How do I use mathematical functions in PHP?

PHP has built-in mathematical functions that can be used to perform more complex math operations. These functions include abs() for finding the absolute value of a number, sqrt() for finding the square root of a number, and pow() for raising a number to a specific power. To use these functions, you simply need to provide the appropriate parameters inside the parentheses.

## 3. Can I perform calculations with variables in PHP?

Yes, you can use variables to store numbers and perform mathematical operations on them in PHP. For example, you can assign the value of 5 to a variable called $num1 and the value of 10 to a variable called $num2. Then, you can use these variables to perform calculations such as $num1 +$num2 to add the two numbers together.

## 4. How can I round a number to a specific decimal place in PHP?

To round a number to a specific decimal place in PHP, you can use the round() function. This function takes two parameters - the number you want to round and the number of decimal places you want to round to. For example, round(3.14159, 2) will round the number 3.14159 to 2 decimal places, resulting in 3.14.

## 5. Is there a way to generate random numbers in PHP?

Yes, PHP has a function called rand() that can be used to generate random numbers. This function takes two parameters - the minimum and maximum values for the range of numbers you want to generate. For example, rand(1, 10) will generate a random number between 1 and 10.