Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying Excel's Circular Reference formula in PHP

I am trying to copy an Excel's Circular Reference formula in PHP.

In Excel I have:

A19 = A25-A22 (result: 8771.65)
A22 = A19*14.1% (result: 1236.80)
A25 = 10000

But, it does not give me correct result when i try to compute it in PHP:

$Tax = 0;
$Gross = 0;    
$Net_Amount = 10000;
$Gross = $Net_Amount - $Tax;
$Tax = $Gross * (14.1/100);

Any idea on how to do this in PHP?

like image 646
m farn Avatar asked May 02 '26 00:05

m farn


1 Answers

By default, Excel will report a warning when you have a circular reference. The exception is if you have told it to handle circular references up to a predefined (you define how many) number of iterations. The way to do the latter in PHP is to use a loop for a predefined number of iterations.

$cycleCount = 12;

$Tax = 0; 
$Gross = 0;     
$Net_Amount = 10000; 
for ($cycle = 0; $cycle < $cycleCount; $cycle++) {
    $Gross = $Net_Amount - $Tax; 
    $Tax = $Gross * (14.1/100); 
}
like image 172
Mark Baker Avatar answered May 04 '26 14:05

Mark Baker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!