Switch function

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
arong
Forum Newbie
Posts: 7
Joined: Tue Apr 19, 2005 9:57 am

Switch function

Post by arong »

hello, Im just curious about the following.

This simple script outputs a different url
each day along the week.

Code: Select all

<?php

//fecha
$fecha = date("m/j/y");

//links

$link1 = "<a href = http://www.google.com>Go to google</a>";
$link2 = "<a href = http://www.gfxartist.com>Go to Gfxartist.com</a>";
$link3 = "<a href = http://www.digitalart.org>Go to digitalart.org</a>";

//switch

switch($fecha){
  case 0:
  print ($link1);
  break;
  
  case 1:
  print ($link2);
  break;
  
  case 3:
  print ($link3);
  break;
}

?>
How can Switch function compare between two days ?
does it have to keep the first day
in a variable to use it later?

thank in advanced
arong
Corvin Gröning
Forum Newbie
Posts: 16
Joined: Tue May 03, 2005 1:02 pm

Post by Corvin Gröning »

a) Please make use of [syntax=php]and[/syntax] and
b) post in the right forum!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

1) You should use [syntax=php][/syntax] tags around your code as it really helps to make things easier to read
2) I'm not too sure what you mean by comparing 2 days
3) All I've ever seen a switch do is just check the value of a single variable. Do a search of the PHP documentation for "switch" and you should be able to find what you need.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

http://www.php.net/switch

i must say i've seen some "weird" usage of switch cases on phpwtf.com etc...

Code: Select all

switch(true)
{
  case ($day1 == $day2):
    // ...
  case ($day > $day2):
    // ...
    break;
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

:arrow: Moved to PHP Code
arong
Forum Newbie
Posts: 7
Joined: Tue Apr 19, 2005 9:57 am

Post by arong »

Sorry I posted on the wrong forum.
this is my code,
I hope this time It can be understandable.

The thing is that I don't know how the switch function can
perform this task.

How can it diferenciate between two dates in order to
output an specific url dependin on a date?

Code: Select all

//fecha
$fecha = date("m/j/y");
 
//links
 
$link1 = "<a href = http://www.google.com>Go to google</a>";
$link2 = "<a href = http://www.gfxartist.com>Go to Gfxartist.com</a>";
$link3 = "<a href = http://www.digitalart.org>Go to digitalart.org</a>";
 
//switch
 
switch($fecha){
  case 0:
  print ($link1);
  break;
  
  case 1:
  print ($link2);
  break;
  
  case 3:
  print ($link3);
  break;
}
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

//fecha
$fecha = date("m/j/y");
 
//links
 
$link1 = "<a href = http://www.google.com>Go to google</a>";
$link2 = "<a href = http://www.gfxartist.com>Go to Gfxartist.com</a>";
$link3 = "<a href = http://www.digitalart.org>Go to digitalart.org</a>";
 
//switch
 
switch($fecha){
  case "2005-05-05":
  print ($link1);
  break;
  
  case "2005-05-06":
  print ($link2);
  break;
}
arong
Forum Newbie
Posts: 7
Joined: Tue Apr 19, 2005 9:57 am

Post by arong »

can you explain how it works ?

It still isn't clear yet for me,
how can it know which url output ?

thanks in advanced
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

switch/case is basically an if/else:

Code: Select all

switch($fecha){
  case "2005-05-05":
  print ($link1);
  break;
  case "2005-05-06":
  print ($link2);
  break;
}

// the above is the same thing as:

if($fecha == "2005-05-05"){
  print ($link1);
}else if($fecha == "2005-05-06"){
  print ($link2);
}
switch case is just easier if you're declaring a lot of variables or doing a lot of scripting within each block.

look here for more help switch
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

or something like

Code: Select all

<?php
 
//fecha
$fecha = date("w");
 
//links
 
$link1 = "<a href = http://www.google.com>Go to google</a>";
$link2 = "<a href = http://www.gfxartist.com>Go to Gfxartist.com</a>";
$link3 = "<a href = http://www.digitalart.org>Go to digitalart.org</a>";
 
//switch
 
switch($fecha){
  case 0:
  case 1:
  case 3:
  print ($link1);
  break;  // show link 1 on Sunday, monday, and wednesday
  
  case 2:
  case 4:
  print ($link2);
  break;  // show link 2 on tuesday and thursday
  
  case 5:
  case 6:
  print ($link3);
  break;  //show link 3 on friday and saturday
}
 
?>
Post Reply