Search code examples
phprefactoringrefactorpro

Idea on refactoring this down?


Can someone explain to me how I can refactor down these math formulas? Is there a fancy way PHP can do these series of math steps in a more condensed way?

PHP

<?php
$s = 0;

   $a = 2300; $b = 1; $c = 34.20; $d = 9.30;  $e = 0.30;

                                        $f = $a * $b;
                                        $g = $f / 100;
                                        $h = $e / 100;                        
                                        $i = $g * $h;
                                        $j = $g - $i;
                                        $k = $j * $d;
                                        $L = $i * $c;
                                        $m = $k + $L;        
                                        $s = $s + $m;  

echo $s;
?>

Results should be : 215.6181 with provided numbers.


Solution

  • You mean something like:

      <?php
      $s = 0;
    
      $a = 2300; $b = 1; $c = 34.20; $d = 9.30;  $e = 0.30;
    
      $g = ($a * $b) / 100;                        
      $k = ($g - ($g * ($e / 100))) * $d;
      $m = $k + (($g * ($e / 100)) * $c);          
      echo $;
      ?>