Confused about a simple switch in PHP (5.5.38) -
i have setup following class mimics 1 working on , trying fix.
the goal
to return fee (value) based on initial value passed in.
the issues
i have 2 issues current code below:
when first value passed in 0 (zero) 12p returned incorrect - should caught first case , return 15p.
i yet tackle want return issuance fee last case satisfied (if next ones not defined). example below; last restriction set
$lower_3
variable - means value passed in greater 31* should return fee of 10p. seeingnull
being returned checks fall through last case.
the code
<?php class issuancescheme { private $lower_1 = 0; private $upper_1 = 20; private $issuancefee_1 = '15p'; private $lower_2 = 21; private $upper_2 = 30; private $issuancefee_2 = '12p'; private $lower_3 = 31; private $upper_3 = null; //50; private $issuancefee_3 = '10p'; private $lower_4 = null; //51; private $upper_4 = null; //75; private $issuancefee_4 = null; private $lower_5 = null; //76; // no $upper_5 private $issuancefee_5 = null; public function calculateissuancefee($volume) { $issuancefee = $this->issuancefee_1; switch ($volume) { case ($volume >= $this->lower_1 && $volume <= $this->upper_1): $issuancefee = $this->issuancefee_1; break; case ($volume >= $this->lower_2 && $volume <= $this->upper_2): $issuancefee = $this->issuancefee_2; break; case ($volume >= $this->lower_3 && $volume <= $this->upper_3): $issuancefee = $this->issuancefee_3; break; case ($volume >= $this->lower_4 && $volume <= $this->upper_4): $issuancefee = $this->issuancefee_4; break; case ($volume >= $this->lower_5): $issuancefee = $this->issuancefee_5; break; } return $issuancefee; } } $issuancescheme = new issuancescheme; // 15p echo sprintf("passed in: %s, exp: %s, got: %s", 0, '15p', $issuancescheme->calculateissuancefee(0)); echo sprintf("passed in: %s, exp: %s, got: %s", 5, '15p', $issuancescheme->calculateissuancefee(5)); echo sprintf("passed in: %s, exp: %s, got: %s", 10, '15p', $issuancescheme->calculateissuancefee(10)); echo sprintf("passed in: %s, exp: %s, got: %s", 15, '15p', $issuancescheme->calculateissuancefee(15)); echo sprintf("passed in: %s, exp: %s, got: %s", 20, '15p', $issuancescheme->calculateissuancefee(20)); // 12p echo sprintf("passed in: %s, exp: %s, got: %s", 25, '12p', $issuancescheme->calculateissuancefee(25)); echo sprintf("passed in: %s, exp: %s, got: %s", 30, '12p', $issuancescheme->calculateissuancefee(30)); // 10p echo sprintf("passed in: %s, exp: %s, got: %s", 50, '10p', $issuancescheme->calculateissuancefee(50)); echo sprintf("passed in: %s, exp: %s, got: %s", 60, '10p', $issuancescheme->calculateissuancefee(60)); echo sprintf("passed in: %s, exp: %s, got: %s", 75, '10p', $issuancescheme->calculateissuancefee(75)); echo sprintf("passed in: %s, exp: %s, got: %s", 100, '10p', $issuancescheme->calculateissuancefee(100));
the result
passed in: 0, exp: 15p, got: 12p // error, expected 15p passed in: 5, exp: 15p, got: 15p passed in: 10, exp: 15p, got: 15p passed in: 15, exp: 15p, got: 15p passed in: 20, exp: 15p, got: 15p passed in: 25, exp: 12p, got: 12p passed in: 30, exp: 12p, got: 12p passed in: 50, exp: 10p, got: // not implemented (see goal 2, , not below) passed in: 60, exp: 10p, got: passed in: 75, exp: 10p, got: passed in: 100, exp: 10p, got:
the second goal hasn't been implemented want return last issuance fee satisfied. in case 10p should returned values passed in greater 31.
you misunderstand switch
statement
switch($var) { case $val: //something
means
if
$var == $val
//something
so in code it's:
switch ($volume) { case ($volume >= $this->lower_1 && $volume <= $this->upper_1): $issuancefee = $this->issuancefee_1; break;
which means
if
$volume == ($volume >= $this->lower_1 && $volume <= $this->upper_1)
$issuancefee = $this->issuancefee_1;
which not true $volume=0
, because ($volume >= $this->lower_1 && $volume <= $this->upper_1)
true
, condition $volume == true
0 == true
.
you need reorganize code if {} elseif {} else {}
statements.
for second issue, change $volume <= $this->upper_1
($this->upper_1===null || $volume <= $this->upper_1)
, , same other levels.
that skip upper limit check if it's set null
(which means unlimited).
public function calculateissuancefee($volume) { $issuancefee = $this->issuancefee_1; if ($volume >= $this->lower_1 && ($this->upper_1===null || $volume <= $this->upper_1)) { return $this->issuancefee_1; } if ($volume >= $this->lower_2 && ($this->upper_2===null || $volume <= $this->upper_2)) { return $this->issuancefee_2; } if ($volume >= $this->lower_3 && ($this->upper_3===null || $volume <= $this->upper_3)) { return $this->issuancefee_3; } if ($volume >= $this->lower_4 && ($this->upper_4===null || $volume <= $this->upper_4)) { return $this->issuancefee_4; } if ($volume >= $this->lower_5) { return $this->issuancefee_5; } }
here's working example: https://3v4l.org/uamib
passed in: 0, exp: 15p, got: 15p passed in: 5, exp: 15p, got: 15p passed in: 10, exp: 15p, got: 15p passed in: 15, exp: 15p, got: 15p passed in: 20, exp: 15p, got: 15p passed in: 25, exp: 12p, got: 12p passed in: 30, exp: 12p, got: 12p passed in: 50, exp: 10p, got: 10p passed in: 60, exp: 10p, got: 10p passed in: 75, exp: 10p, got: 10p passed in: 100, exp: 10p, got: 10p
Comments
Post a Comment