How to find the percentage a number is of another number in PHP?
Example
$num1 = 2.6;
$num2 = 2.6;
// Should equal to 100%
Divide the two numbers and multiply by 100 to get the percentage:
$percentage = ($num1 / $num2) * 100;
echo $percentage . "%";
Of course you will need to check so that $num1
is not 0, something like this: $percentage = ($num1 !== 0 ? ($num1 / $num2) : 0) * 100;
$percentage = sprintf("%d%%", $num1 / $num2 * 100); // string: 100%
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With