<?php $y="+"; $x=(10 $y 10); //$x=(10+10); echo $x; ?> PHP: above is my assume, of course it's error. i'd like to do a switch "+" "-" how to fix it, echo $x will be 20 thank you
$y must be "+"; due to i 'd like to switch it mean i am just change "+" to be "-" $x will be 0 if change "+" to be "*" $x will be 100
<?php $y='+'; $x=0; switch($y) { case '+': $x=10+10; break; case '*': $x = 10 * 10; break; } echo $x; ?> Code (markup):
thank you but it seems can't using in my case if($a+$b) { } PHP: i'd like to switch if($a+$b) { } to be below if($a-$b) { } just change + symbols but +- seems unable to be variable as far as i know.
Eval is too heavy. There is a much simpler solution. Use a simple multiplier initialized with 1 (for adding) or -1 (for subtraction). Example: <?php if ($subtract) $mul = -1; else $mul= 1; $x = 10 + $mul*10; echo $x; ?> PHP:
you're getting your cases mixed up , what you want to do is check $y and then based on that you change the operation appropriately.