Unlike integers and floats, addition and subtraction of several fractions (rational number) require finding least common multiple (LCM) of the denominators of those fractions. Also, simplifying a fractions requires finding greatest common divisor (GCD) of both numerator and denominator of that fractions.
Here, we quote the PHP codes to determine the numerator and denominator of a fraction. Note that both of the following functions recall the function FracCheck() which is included in the previously posted related articles.
function FracNumer($A)
{
if(FracCheck($A)==0) { return NULL; }
else
{
$i=0;
$len=strlen($A)-1;
while($A[$i]!="/" && $i<=$len)
{
$Numer=$Numer.$A[$i];
$i++;
}
return $Numer;
}
}
function FracDenom($A)
{
if(FracCheck($A)==0) { return NULL; }
else
{
$i=0; $len=strlen($A)-1;
while($A[$i]!="/" && $i<=$len)
{
$i++;
}
if($i>$len) { return 1; }
else
{
$i++;
while($i<=$len)
{
$Denom=$Denom.$A[$i];
$i++;
}
return $Denom;
}
}
}