Page 1 of 1

switch statement: check for multiple values[SOLVED]

Posted: Wed Aug 31, 2005 11:42 am
by raghavan20
I am trying to write a function which would return the number of days in a month.
I am trying to match $this->month(a month value, numeric or string) with multiple values in a case.
If I give 1,2 or FEB it return 31 all the time.

Code: Select all

function findDaysOfMonth(){
		echo "current month:".$this->month;
		switch($this->month){
			case ("JAN" || "JANUARY" || 1):
				return 31;
				break;
			case ("FEB" || "FEBRUARY" || 2):
				if ($this->year%4 == 0){
					return 29;
				}else{
					return 28;
				}
				break;
			case ("MAR" || "MARCH" || 3):
				return 31;
				break;
			case ("APR" || "APRIL" || 4):
				return 31;
				break;
		}
	}
2. Is there a function to get the day(like mon, tue, wed) for the first day in the month??? :?

Posted: Wed Aug 31, 2005 11:47 am
by nielsene
You probably want to do something like:

Code: Select all

switch ($this->month) {
  case "JAN":
  case  "JANUARY":
  case "MAR":
  case "MARCH:
  // ...
  case 1: return 31; break;
  case "FEB":
  case "FEBRUARY":
  case 2: return 28: break;
}
or even better, only list the non 31 ones and use default to catch all those....

The way you coded it was effectively case (TRUE || TRUE || TRUE): --> case TRUE
so it $this->month was non-zero it matched.

Posted: Wed Aug 31, 2005 11:56 am
by John Cartwright

Code: Select all

date('t',time());
will give you the amount of days per month.

Posted: Wed Aug 31, 2005 11:59 am
by The Monkey
I might look at date('t') (days in month) and mktime...

Posted: Wed Aug 31, 2005 12:00 pm
by raghavan20
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;
		}
	}

and for the second question...here is the function

Code: Select all

function getWeekDay($day, $month, $year){
	$timestamp = mktime(0, 0, 0, $month, $day, $year);
	$date = getdate($timestamp);
	$wkday = $date["weekday"];
	return $wkday;
}

Posted: Wed Aug 31, 2005 12:10 pm
by raghavan20
looks like this function as suggested takes off the work ....

Code: Select all

function findDaysOfMonth($month, $year){
	$timestamp = mktime(0, 0, 0, $month, 0, $year);
	$days = date("t", $timestamp);
	return $days;
}

Posted: Wed Aug 31, 2005 12:27 pm
by nielsene
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.