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
arong
Forum Newbie
Posts: 7 Joined: Tue Apr 19, 2005 9:57 am
Post
by arong » Thu May 05, 2005 10:09 am
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 » Thu May 05, 2005 10:11 am
a) Please make use of [syntax=php]and[/syntax] and
b) post in the right forum!
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Thu May 05, 2005 10:13 am
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 » Thu May 05, 2005 11:22 am
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;
}
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Thu May 05, 2005 12:24 pm
Moved to PHP Code
arong
Forum Newbie
Posts: 7 Joined: Tue Apr 19, 2005 9:57 am
Post
by arong » Thu May 05, 2005 1:24 pm
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 » Thu May 05, 2005 1:28 pm
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 » Thu May 05, 2005 2:02 pm
can you explain how it works ?
It still isn't clear yet for me,
how can it know which url output ?
thanks in advanced
Burrito
Spockulator
Posts: 4715 Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah
Post
by Burrito » Thu May 05, 2005 4:31 pm
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
phpScott
DevNet Resident
Posts: 1206 Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.
Post
by phpScott » Fri May 06, 2005 5:20 am
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
}
?>