switch statement: check for multiple values[SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

switch statement: check for multiple values[SOLVED]

Post 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??? :?
Last edited by raghavan20 on Wed Aug 31, 2005 12:11 pm, edited 1 time in total.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

date('t',time());
will give you the amount of days per month.
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

I might look at date('t') (days in month) and mktime...
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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;
}
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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;
}
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
Post Reply