raghavan20 wrote:nielsene, this is how they did in one of the examples i was and it works.
what is the advantage of using "==="?
where do we have to use???
Code: Select all
function findDaysOfMonth(){
echo "current month:".$this->month;
switch($this->month){
case ($this->month==="JAN" || $this->month==="JANUARY" || $this->month===1):
return 31;
break;
case ($this->month==="FEB" || $this->month==="FEBRUARY" || $this->month===2):
if ($this->year%4 == 0){
return 29;
}else{
return 28;
}
break;
case ($this->month==="MAR" || $this->month==="MARCH" || $this->month===3):
return 31;
break;
case ($this->month==="APR" || $this->month==="APRIL" || $this->month===4):
return 31;
break;
}
}
First, yes the date function is the better choice here.
However to address the switch issue...
Switch is basically just syntactic sugar:
Code: Select all
switch ($foo) {
case "bar" : ... break;
case "baz": ... break:
default: break;
}
// is "basically"
$temp = $foo;
if ($temp=="bar") {
} else if ($temp=="baz") {
} else {
}
Its not
exactly that because of the behavoir in the absense of "break"s.
However as you can see the case statement already IS the equality test.
In your first version ("JAN" || "JANUARY" || 1) PHP will basically do
Code: Select all
if ($this->month == ("JAN" || "JANUARY" || 1)
non-empty string || non-empty-string || 1 ---> True || True || true
so you have if ($this->month == TRUE).
The later example, using === and inside the case behaves as follows
Code: Select all
case ($foo === "Bar" || $foo==="Baz"):
case (True || False)
case (true) : (or false depending on the value of $foo)
Now the switch's variable is compared to a boolean. So if the switch's variable is non-empty it will match the first case that returned true. So it "works" but its doing lots of extra work and is rather blantatly abusing the switch statement.
In general I would strong advise only using simple strings, numbers, or defined constants inside a case. Generally speaking placed equality checked, or function calls or variables is a sign that something isn't right.