3

I made a calculation in Excel and am trying to use it in PHP. Unfortunately, I do not get the correct total when I'm using the function as I wrote it.

This is the calculation as I am using it in Excel :

2500 / (( 0.25 / 100 ) / ( 1 - ( ( 1 + ( 0.25 / 100 )) ^ -360)))

The function I made for calculating in PHP:

function calculatesum($income, $interestmonthly, $months){
   return $income / (( $interestmonthly / 100 ) / ( 1 - ( ( 1 + ( $interestmonthly / 100 ) ) ^ -$months)));
}

The function in PHP returns: '360000000' . But this should be '592.973,4538' .

I'm wondering what I'm doing wrong. Any tips will be welcome!

Already looked into the 'pow' functions, but still getting the same outcome.

2
  • what are you using "^" for in your code? Commented Jan 12, 2016 at 14:37
  • @user5237857 , the ^ is for the power of a number. (for example 2^5 = 2*2*2*2*2) Commented Jan 12, 2016 at 16:55

3 Answers 3

4

Excel uses ^ for calculating the power of a number. PHP uses pow($number, $exponent) for that purpose.

function calculatesum($income, $interestmonthly, $months){
  return $income / (( $interestmonthly / 100 ) / ( 1 - ( pow(( 1 + ( $interestmonthly / 100 ) ), -$months))));
}

echo calculatesum(2500,0.25,360);

Output:

592973.4537607

See http://php.net/manual/en/function.pow.php for documentation and examples.

Sign up to request clarification or add additional context in comments.

2 Comments

I see you probably solved the issue here so my answer is irrelevant.
Hey @maxhb many thanks for your solution. It works like a charm. I will look into the pow-function.
1

Use this. PHP does not raise power using ^. You can raise power of a value using

**

For example,

2**5 will be equal to 2^5

Hope this helps.

function calculatesum($income, $interestmonthly, $months){
   return $income / (( $interestmonthly / 100 ) / ( 1 - ( ( 1 + ( $interestmonthly / 100 ) ) ** -$months)));
}

Comments

0

PHP doesn't consider ^ as an operator, instead it uses another function pow. enter link description here I think that is all you have to change to get the value you are looking for.

function calculatesum($income, $interestmonthly, $months){ 
   $n1=( $interestmonthly / 100 );
   $n2= ( 1 + ( $interestmonthly / 100 ) );
   $r1= pow($n2,-$months);
   $r2=$n1 / ( 1 - $r1);
   return $income /($n1 /$r2);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.