1

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?

1
  • 1
    Use $mid_val * 0.35. % is for mod. Commented Mar 20, 2013 at 8:26

4 Answers 4

3

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

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

1 Comment

Thank you for giving the right answer and also explaining where I was going wrong :)
3

You cannot use % as a math function:

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

Comments

1

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

Comments

1

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.

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.