-2

My code:

<?php

    function ci($principle, $rate, $time) {

        $ci = ($principle * (( (1 + $rate / 100) ** $time) - 1));
        echo $ci;

    }

?>
<?php
    echo ci(10,10,10);
?>

And when I am running it, it gives the following error

Parse error: syntax error, unexpected '*' in D:\Xampp\htdocs\php\functions.php on line 4

Please tell me what's the error in line 4 ($ci = ($principle * (((1+$rate/100)**$time)-1));) ?

3
  • Is it intentional with the double * in )**$time) ?
    – Epodax
    Commented Oct 2, 2015 at 10:38
  • it's a global issue raised by you as political mean as 'PHP' is specially not giving correct answer to you..read how to ask question on SO
    – lakshman
    Commented Oct 2, 2015 at 10:40
  • FYI: You don't return anything in your function, so the echo call for the return value is unnecessary.
    – Rizier123
    Commented Oct 2, 2015 at 10:55

2 Answers 2

4

Your syntax as it is, is correct. The problem is your PHP version. The ** operator was introduced in PHP 5.6 and you probably have something below.

So either update your PHP or use pow().

2
2

OP had an extra * over

(1 + $rate / 100) ** $time)

which results into PHP syntax error Unexpected * within PHP verison < 5.6.0 and works fine for the higher versions

function ci($principle, $rate, $time) {
    $ci = ($principle * (((1 + $rate / 100) * $time) - 1));
                                         //^^ removed extra *
    echo $ci;
}

ci(10, 10, 10);

Demo

2
  • Can you elaborate a bit more your answer?
    – Amarnasan
    Commented Oct 2, 2015 at 10:42
  • Updated answer @Amarnasan Commented Oct 2, 2015 at 10:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.