if statement within switch not working as expected

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
rblumberg
Forum Newbie
Posts: 1
Joined: Sat May 31, 2008 12:15 pm

if statement within switch not working as expected

Post by rblumberg »

I'm a newbie with regard to PHP, and I'm experiencing a problem that I can't resolve.

The following code segment (line-numbered for convenience only), is intended to produce a different result when it's run on a weekday, a Saturday, or a Sunday, and, if it runs on a weekday, a different result if it's run before 8:30PM or after.

The problem I'm having is on line 19. If the function runs on a weekday, before 8:30PM, the function is_evening() returns false, as it should, but the code in the if statement (lines 20-24) runs anyway (i.e. it echos the text of the file 'evening.txt').

When I rewrite the whole thing to avoid the switch statement, using sequence of if/elseif/else statements, it works as expected. But why is it not working correctly here?

Can anyone tell me where the error is in the code?

Thanks.

Richard

Code: Select all

1  function is_evening()   {
 2          date_default_timezone_set('America/New_York');
 3          return (date("G") * 60 + date("i")) > (20*60 + 30);
 4  }
 5  
 6  date_default_timezone_set('America/New_York');
 7  $this_day = strtolower(date("l"));
 8  
 9  switch ($this_day)      {
10          case "saturday":
11          case "sunday":
12                  if (!file_exists("$this_day.txt")) :
13                          echo "Error: could not find file '$this_day.txt'.";
14                  else :
15                          echo file_get_contents("$this_day.txt");
16                  endif;
17                  break;
18          default:
19                  if (is_evening()) :
20                          if (!file_exists("evening.txt")) :
21                                  echo "Error: could not find file 'evening.txt'.";
22                          else :
23                                  echo file_get_contents("evening.txt");
24                          endif;
25                  else :
26                          $filename = date("Ymd.\t\x\t") . ".txt";
27                          if (file_exists($filename)) :
28                                  echo make_table(file_get_contents($filename));
29                          elseif (!file_exists("morning.txt")) :
30                                  echo ("Error: could not find file 'morning.txt'");
31                          else :
32                                  echo (file_get_contents("morning.txt"));
33                          endif;
34                  endif;
35                  break;          
36  }
Mr.RED
Forum Newbie
Posts: 8
Joined: Sat May 31, 2008 1:23 pm

Re: if statement within switch not working as expected

Post by Mr.RED »

Is that an exact copy of your code?

I don't think thats the right way to do IF statments, dont know why your using colons...

This is the correct method:

Code: Select all

 
19                  if (is_evening()){
 
25                  }else{
 
34                  }
 
Hope that fixes it!
Post Reply