Page 1 of 1

Switch function

Posted: Thu May 05, 2005 10:09 am
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

Posted: Thu May 05, 2005 10:11 am
by Corvin Gröning
a) Please make use of [syntax=php]and[/syntax] and
b) post in the right forum!

Posted: Thu May 05, 2005 10:13 am
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.

Posted: Thu May 05, 2005 11:22 am
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;
}

Posted: Thu May 05, 2005 12:24 pm
by Chris Corbyn
:arrow: Moved to PHP Code

Posted: Thu May 05, 2005 1:24 pm
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;
}

Posted: Thu May 05, 2005 1:28 pm
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;
}

Posted: Thu May 05, 2005 2:02 pm
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

Posted: Thu May 05, 2005 4:31 pm
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

Posted: Fri May 06, 2005 5:20 am
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
}
 
?>