1

Ok so i have 3 variables

 $real_price = 33.00; $real_option = 13.00; $option_value['price_prefix'] = "-"; $real_total = $real_price $option_value['price_prefix'] $real_option; 

how come i cant do line 4

I need $real_total to be 20.00

i get Parse error: syntax error, unexpected T_VARIABLE

8
  • Why should this work? How can the PHP interpreter know what to do when you put three variables in a row? Commented Sep 3, 2011 at 12:25
  • 3
    See stackoverflow.com/questions/2552052/variable-operators-in-php Commented Sep 3, 2011 at 12:29
  • @str: The solution provided there is a bad solution. Commented Sep 3, 2011 at 12:33
  • @Rok Kralj No, it's a good solution that only allows a certain list of operators to be used. Commented Sep 3, 2011 at 12:41
  • @Igor: Refresh page more often. Solution listed there is problematic from the point of relying on unstandard extension & performance hit. Commented Sep 3, 2011 at 12:45

1 Answer 1

2
 $real_price = 33.00; $real_option = 13.00; $option_value['price_prefix'] = "-"; eval('$real_total = $real_price '.$option_value['price_prefix'].' $real_option'); 

Althrough I don't recommend doing it like that. Do it like this:

 $real_price = 33.00; $real_option = 13.00; $option_value['price_prefix'] = "-"; switch($option_value['price_prefix']) { case '+': $real_total=$real_price+$real_option; break; case '-': $real_total=$real_price-$real_option; break; case '*': $real_total=$real_price*$real_option; break; } 
Sign up to request clarification or add additional context in comments.

3 Comments

For a quick answer :) I wanted to present how can OP achieve result his way, but presenting nicer alternative.
The hero with downvote shall present his reasons for doing so.
I have removed the downvote because you edited the answer and now it's all better. Edit: Oops, it seems that I can't remove my downvote. Sorry.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.