Ajit S
Ajit S

Reputation: 1341

custom math function using php

I'm not so good with PHP. I have a function which I'm trying to execute, but am unable to do so. I'm trying this for quite some time now.

$init_val = ($price/120) * 20;
$mid_val = $price - $init_val;
//$final_value = $mid_value - 35% of $mid_val     // want to execute this logic
//$final_value = $mid_value - ($mid_val * 35%);   // this gives error
//$final_value = $mid_value - 35%;                // same error

The error given is:

PHP Parse error: syntax error, unexpected ';' in /var/www/site1/function.php on line 51

What am I doing wrong?

Upvotes: 1

Views: 155

Answers (4)

Déjà vu
Déjà vu

Reputation: 28850

If you want to code the idea of percent, create a function

function percent($value) {
    return $value / 100;
}

then you can write

$final_value = $mid_value - $mid_val * percent(35);

(no need of parentheses around $midval * percent(35) since * has a higher precedence than - (see this))

% is the modulus operator.

Upvotes: 1

Matt Cain
Matt Cain

Reputation: 5768

You can't use percentage values like that in PHP. The % symbol is reserved for modulus operations. Try this instead:

$final_value = $mid_value - ($mid_val * 0.35);

That would give you 65% of $mid_value in $final_value

Upvotes: 3

Mihai Iorga
Mihai Iorga

Reputation: 39724

You cannot use % as a math function:

$final_value = $mid_value - ($mid_val * 0.35);

Upvotes: 3

Rob
Rob

Reputation: 4947

$final_value = $mid_value - ($mid_val * 0.35);

Upvotes: 1

Related Questions