How to Calculate the Ratio Between Two Numbers By PHP With Example?

Hello friends today i will tell you how you can calculate ratio by php using 2 number. So let’s go.

Ratio : The ratio is always in two homogeneous zodiacs. A ratio is obtained by dividing one amount into another. Like if A amount is divided by B amount.Then A / B is called the ratio of A and B. Write it as follows.
A/B = A:B
Therefore, in the ratio A: B, A is called the first term and B is called the second term. If each term of a ratio is multiplied or divided by a fixed number except 0, then that ratio remains unchanged.

Calculate Ratio PHP Code

<?php
function find_ratio($num1, $num2){ for($i = $num2; $i > 1; $i--) { if(($num1 % $i) == 0 && ($num2 % $i) == 0) { $num1 = $num1 / $i; $num2 = $num2 / $i; } } return "$num1:$num2"; } echo find_ratio(2, 6), '<br>'; echo find_ratio(2, 8), '<br>';
?>

How to use ratio calculator in php file?

You have been told the php code to calculate the ratio above. Now I will tell you step to step how you will use the code to calculate the ratio in the php file.First of all you create a php file. You can name this file in your own way like ratio.php.In this file, you add the code of html. The way is described below.

ratio.php

<html>
<head>
<meta charset="utf-8">
<title>Ratio Calculator</title>
</head>
<body>
<form method="post">
<p>First Value:<br/>
<input type="text" name="f"></p>
<p>Second Value:<br/>
<input type="text" name="s"></p>
<button type="submit" name="submit" value="Calculate">Calculate Ratio</button>
</form>
</body>
</html>

Now how do you calculate the ratio by php. Its code is written well below. You have to use this code in the file of ratio.php. First of all, understand how the ratio is calculated.

<?php 
if (isset($_POST['submit'])) {
$a = $_POST['f'];
$b = $_POST['s'];
if(empty($a) && empty($b)) {
echo "empty";
} else {
function find_ratio($num1, $num2){
for($i = $num2; $i > 1; $i--) {
if(($num1 % $i) == 0 && ($num2 % $i) == 0) {
$num1 = $num1 / $i;
$num2 = $num2 / $i;
}
}
return "$num1:$num2";
}
echo find_ratio($a, $b), '<br>';
}
}
?>

So far, whatever you have been told above, you may have understood all the topics. Now you have to add this code to a single file. The example of which is given below. You can copy the code of this file to your php You can use it by adding it to the file.

Ratio.php with proper code

<html>
<head>
<meta charset="utf-8">
<title>Ratio Calculator</title>
</head>
<body>
<form method="post">
<p>First Value:<br/>
<input type="text" name="f"></p>
<p>Second Value:<br/>
<input type="text" name="s"></p>
<button type="submit" name="submit" value="Calculate">Calculate Ratio</button>
</form>
</body>
</html>
<?php
if (isset($_POST['submit'])) {
$a = $_POST['f'];
$b = $_POST['s'];
if(empty($a) && empty($b)) {
echo "empty";
} else {
function find_ratio($num1, $num2){
for($i = $num2; $i > 1; $i--) {
if(($num1 % $i) == 0 && ($num2 % $i) == 0) {
$num1 = $num1 / $i;
$num2 = $num2 / $i;
}
}
return "$num1:$num2";
}
echo find_ratio($a, $b), '<br>';
}
}
?>

In the code above, there are two examples showing you how this function works.I hope that you found this function as useful as I did.