# Calculate Net Present Value (NPV) in PHP

In this guide, we’ll show you how to calculate the Net Present Value (NPV) of a series of future cashflows using a PHP script.

## Net Present Value Overview

Using a little financial maths, we find the Net Present Value of a cashflow at a future time, and interest rate by: \[NPV = \frac{X}{(1+i)^{t}}\]

So, for example, the NPV of a payment of $100 in 2 years’ time with an (annually compounding) interest rate of 5% can be found using the above formula with the following values:

- \(X = 100\)
- \(t = 2\)
- \(i = 0.05\)

So we have: \[NPV = \frac{100}{(1.05)^{2}} = 90.70\]

This reflects the idea of compound interest where:

- $90.70 invested now would earn interest at 5% in the first year to grow to after one year
- Then if left invested, both the original amount and the first year’s interest would earn interest in a second year. This causes it to grow to
- We could also have verified this by:

So if we can invest $90.70 now, at 5% interest, it would grow to $100 in 2 years’ time. Thus we say that $90.70 is the *present value* of $100 in 2 years’ time.

We can implement this in PHP using the following simple script. Note we use the `pow(base, exp)`

function to raise one number (the *base*) to the power of another (the *exponent*).

## NPV of a series of yearly cashflows

Expanding on the above, suppose we have cashflows paid at yearly intervals, starting immediately, and that the interest rate is still 5%. We can calculate the NPV of all cashflows by calculating the present value of each one individually, then adding them all up.

\[NPV = \sum_{t=0}^{n} \frac{X_t}{(1+i)^{t}}\]

We’ll build a function to do this in PHP:

Breaking it down, this function takes arguments `$cashflows`

- an array of cashflows - and `$intRate`

- the interest rate at which to value the cashflows. It does the following calculation steps:

- Use a variable
`$npv`

to keep track of the running total, which we initialise to zero. - Loop through the
`$cashflows`

array, calculating the present value of each using the formula above, and adding that to the total,`$npv`

. - Return the value
`$npv`

.

## NPV of a series of cashflows at different times

Suppose the cashflows you’re interested in don’t occur at exactly yearly intervals, but at various other points in the future. It’s simple to adjust the above example to cope with this, and we can just add an extra array `$times`

as an argument to the function, and use this when calculating the present value of each cashflow.

We use this function to replicate the result of our `npv()`

function created above, and then to calculate the NPV with different timings.

Hope this helps. As always if you have any comments please add them in the comments box below.

## Comments