joe48182 Posted January 21, 2007 Share Posted January 21, 2007 I have been learning php for like 2 days now, and I got a couple of things down, and I am just trying to write simple script to display a different message depending on the day, so... heres my code -[code]<?php$d=Date("D");$s="Have a good day sir on this cool Sunday!";$m="Have a nice Monday dude!";$t="Its Tuesday and have a nice day!";$w="Wendsday! I'll bet your bored!?";$th="Thursday's cool aint it?";$f="Fridays rule!!!!";$sa="Saturdays are cool too!!!!";if ($d=="Sun"){echo $s;}elseif ($d=="Mon"){echo $m;}elseif ($d=="Tue"){echo $t;}elseif ($d=="Wen"){echo $w;}elseif ($d=="Thu"){echo $th;}elseif ($d=="Fri"){echo $f;} elseif ($d=="Sat") {echo $sa;} ?>[/code]So... can someone tell me whats wrong with this? I a have been following the tutorials on w3schools, and I am confused! lol. Quote Link to comment https://forums.phpfreaks.com/topic/35080-help-a-php-newb-please/ Share on other sites More sharing options...
paul2463 Posted January 21, 2007 Share Posted January 21, 2007 whats wrong with it? you could try a switch statement but if you are following tutorials then that will come up later probablyit works on my system Quote Link to comment https://forums.phpfreaks.com/topic/35080-help-a-php-newb-please/#findComment-165547 Share on other sites More sharing options...
joe48182 Posted January 21, 2007 Author Share Posted January 21, 2007 hmmm.... Thats weird I guess... I get a error on my server :Phttp://thehauntedlair.com/SECRET/php/days.php Quote Link to comment https://forums.phpfreaks.com/topic/35080-help-a-php-newb-please/#findComment-165548 Share on other sites More sharing options...
redbullmarky Posted January 21, 2007 Share Posted January 21, 2007 what's the issue you're having? the only thing i can see that's out of place (but not 100% sure its important but worth a try) is Date. Try and change the function name to ALL lowercase[code]$d=date("D");[/code]also you may want to look at the switch/case method, as it removes the need for so many ifs/elses and makes things easier to read:[code]<?phpswitch ($d){ case 'Sun': echo $s; break; case 'Mon': echo $m; break;}?>[/code]you can then even take things further by using an array (a group of variables). so:[code]<?php$message['Sun'] = "This is Sunday";$message['Mon'] = "This is Mondays message";$message['Tue'] = "Tuesday, anyone?";...etc...etc...$d = date("D");echo $message[$d];?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/35080-help-a-php-newb-please/#findComment-165549 Share on other sites More sharing options...
paul2463 Posted January 21, 2007 Share Posted January 21, 2007 [code]<?php$d=Date("D");$s="Have a good day sir on this cool Sunday!";$m="Have a nice Monday dude!";$t="Its Tuesday and have a nice day!";$w="Wendsday! I'll bet your bored!?";$th="Thursday's cool aint it?";$f="Fridays rule!!!!";$sa="Saturdays are cool too!!!!";switch($d){ case "Sun": echo $s; break; case "Mon": echo $m; break; case "Tue": echo $t; break; case "Wed": echo $w; break; case "Thu": echo $th; break; case "Fri": echo $f; break; case "Sat": echo $sa; break; }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/35080-help-a-php-newb-please/#findComment-165551 Share on other sites More sharing options...
joe48182 Posted January 21, 2007 Author Share Posted January 21, 2007 Well paul2463, I just tried what you said right before you edited that and changed the last elseif to just else and it works now!Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/35080-help-a-php-newb-please/#findComment-165552 Share on other sites More sharing options...
paul2463 Posted January 21, 2007 Share Posted January 21, 2007 I thought it might but as it went onto switches I put the code for the switch in for youthe elseif system needs a finishing else or it does not complete what it needs to do Quote Link to comment https://forums.phpfreaks.com/topic/35080-help-a-php-newb-please/#findComment-165553 Share on other sites More sharing options...
448191 Posted January 21, 2007 Share Posted January 21, 2007 Avoid both big switch and/or ifelse statements.[code]<?php$messages = array( 'Sun'=>"Have a good day sir on this cool Sunday!", 'Mon'=>"Have a nice Monday dude!", 'Tue'=>"Its Tuesday and have a nice day!", 'Wen'=>"Wendsday! I'll bet your bored!?", 'Thu'=>"Thursday's cool aint it?", 'Fri'=>"Fridays rule!!!!", 'Sat'=>"Saturdays are cool too!!!!");echo $messages[Date("D")];?>[/code][b]Edit:[/b] Mark already said that.. ::) Quote Link to comment https://forums.phpfreaks.com/topic/35080-help-a-php-newb-please/#findComment-165561 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.