Yohan Greenburg Yohan Greenburg - 3 years ago 62
PHP Question

Calculations In PHP

I am needing to perform a calculation and round the results all in my php. I have tried the below but I always end up with a 0. Which if I manually perform the calculation should not be the case. Below is my syntax and what I am after is

val1-val2
answer from above / val1
answer from above * 100 (to get percent)


Here is the syntax I use

print "<td>" . number_format(round($res->val1-$res->val2/$res->val1)*100) . "%" . "</td>";


edit

Let's use real life example
$val1 = 1122
$val2 = 280

I want the calculation steps to be $val1-$val2 = 842

Then 842/$val1 or 842/1122 = .75

If I use
($val1-$val2)/$val1 = 1 which is incorrect.

Answer Source
echo round((($val1 - $val2) / $val1) * 100).'%';

Works fine? See here https://3v4l.org/d4bpe

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download