Afficher un message
Vieux 17/09/2007, 15h49   #1
Paul Hunter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Switch statement not recognizing compare value

I'm new to PHP, and I'm stuck. I created the below class, and the switch
statement in Calculator.calculate() doesn't find a successful match for any
of the case values. I've checked with echo's, and the values are right (no
extra whitespace). What am I not doing right?

Thanks,
Paul

class Calculator {

var $operand1;
var $operand2;
var $operator;
var $result = "no result";

function Calculator ($o1, $o2, $op) {
$this->operand1 = $o1;
$this->operand2 = $o2;
$this->operator = $op;
} // Calculator

function calculate () {
switch ($operator) {
case "+":
$result = $op1 + $op2;
break;
case "-":
$result = $op1 - $op2;
break;
case "*":
$result = $op1 * $op2;
break;
case "/":
$result = $op1 / $op2;
break;
case "div":
$result = (int)$op1 / (int)$op2;
break;
case "mod":
$result = $op1 % $op2;
break;
default:
echo "calculate: operator not found<br>";
break;
} // switch

return $result;
} // calculate
} // class Calculator



  Réponse avec citation
 
Page generated in 0,05126 seconds with 9 queries