leisnerr Posted March 18, 2007 Share Posted March 18, 2007 Hey guys, I'm attempting to learn php..I know this is more of an advanced forum but I am a little confused. I'm just following some stuff on w3schools.com. Now 1st I tried typing in this code the way I thought it should go, then I checked it with what they had and it was the same. But when I throw it on my server it has it has an error. This is the link to the page on my server: http://zonedhosting.com/newtest.php Here is the code I'm working with: <html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; elseif ($d=="Sun"); echo "Have a nice Sunday!"; else echo "Have a nice day!"; ?> </body> </html> It is telling me that my 'else' statement has an unexpected T_ELSE from a syntax error. But like I said I ended up coming out with the same code as w3schools. Any suggestions? -Ross Quote Link to comment https://forums.phpfreaks.com/topic/43259-quick-noob-question/ Share on other sites More sharing options...
Steppio Posted March 18, 2007 Share Posted March 18, 2007 I'm not sure whether this is the source of the trouble, but on the line with your elseif statement you have a ; following it. elseif ($d=="Sun"); <---- here echo "Have a nice Sunday!"; Quote Link to comment https://forums.phpfreaks.com/topic/43259-quick-noob-question/#findComment-210027 Share on other sites More sharing options...
interpim Posted March 18, 2007 Share Posted March 18, 2007 <?php $d=date("D"); if ($d=="Fri"){ echo "Have a nice weekend!"; }elseif ($d=="Sun"){ echo "Have a nice Sunday!"; }else{ echo "Have a nice day!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/43259-quick-noob-question/#findComment-210035 Share on other sites More sharing options...
cmgmyr Posted March 18, 2007 Share Posted March 18, 2007 yes, i think that was the only thing doing it. I think it's also good to enclose your statements in {}'s just incase your statement gets bigger later you won't have to worry about it. Like: <?php $d=date("D"); if ($d=="Fri"){ echo "Have a nice weekend!"; }elseif ($d=="Sun"){ echo "Have a nice Sunday!"; }else{ echo "Have a nice day!"; } ?> hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/43259-quick-noob-question/#findComment-210036 Share on other sites More sharing options...
leisnerr Posted March 18, 2007 Author Share Posted March 18, 2007 Yea that was the problem, the weird thing is that my server was telling me the syntax error was on a different line. Quick question though..what is the significance of using the { } to break up your statements? I'm JUST learning the basics as we speak so bare with me lol. Quote Link to comment https://forums.phpfreaks.com/topic/43259-quick-noob-question/#findComment-210042 Share on other sites More sharing options...
cmgmyr Posted March 18, 2007 Share Posted March 18, 2007 It will usually always tell you the wrong line, because it gives you an error on where your problem "became" a problem...not where it started. So whenever you get an error like that you will have to backtrack a little. You always want to break up your statements because it looks better and is easier to read and as I said before if you add more then one line in the statement you will have to use them anyways. But you should just use them because it is "good programming practice" So if you get into a habbit of using them now...you will always use them. Quote Link to comment https://forums.phpfreaks.com/topic/43259-quick-noob-question/#findComment-210044 Share on other sites More sharing options...
leisnerr Posted March 18, 2007 Author Share Posted March 18, 2007 Oh ok. On a side note, I'm reading about switches and such now and it just seems that w3schools isnt really explaining everything about it to help me understand it. Does anyone know of more informative website I could be learning from? Since I didn't really understand what switches were doing from them I browsed around and found a more helpful spot but I'm still a bit confused. I understand that depending on what the variable equal determines how many lines it will echo, but once again the code from w3schools isnt working properly. They told me to write this: <html> <body> <?php if ($x==1) { echo "X is equal to 1"; } elseif ($x == 2) { echo "X is equal to 2"; } elseif ($x == 3) { echo "X is equal to 3"; } switch ($x) { case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; case 3: echo "Number 3"; break; default: echo "No number between 1 and 3"; } ?> </body> </html> I can only get the default echo from that. When I browsed I came across this code: <html> <body> <?php if ($i == 0) { echo "i equals 0"; } elseif ($i == 1) { echo "i equals 1"; } elseif ($i == 2) { echo "i equals 2"; } switch ($i) { case 0: echo " i equals 0"; break; case 1: echo " i equals 1"; break; case 2: echo " i equals 2"; break; } ?> </body> </html> When I use the 2nd one I get a consistent i is equal to 0. Just to clarify it is giving me a result of 0 because it is the first command line correct? And the break; stop's it from posting the rest of the commands after the one that it is directed? Quote Link to comment https://forums.phpfreaks.com/topic/43259-quick-noob-question/#findComment-210049 Share on other sites More sharing options...
shaunrigby Posted March 18, 2007 Share Posted March 18, 2007 The break ends the case, plus try using www.php.net for your commands they explai them much better Quote Link to comment https://forums.phpfreaks.com/topic/43259-quick-noob-question/#findComment-210065 Share on other sites More sharing options...
DeathStar Posted March 18, 2007 Share Posted March 18, 2007 Hav'nt made code like that in a loong time... really good for beginning to use php! Quote Link to comment https://forums.phpfreaks.com/topic/43259-quick-noob-question/#findComment-210079 Share on other sites More sharing options...
cmgmyr Posted March 18, 2007 Share Posted March 18, 2007 <?php $i = 1; switch ($i) { case 0: echo " i equals 0"; break; case 1: echo " i equals 1"; break; case 2: echo " i equals 2"; break; } ?> echos "i equals 1" Quote Link to comment https://forums.phpfreaks.com/topic/43259-quick-noob-question/#findComment-210087 Share on other sites More sharing options...
leisnerr Posted March 18, 2007 Author Share Posted March 18, 2007 Oh ok, they weren't telling me to define a variable earlier on. That makes a bit more sense now lol thanks. Quote Link to comment https://forums.phpfreaks.com/topic/43259-quick-noob-question/#findComment-210102 Share on other sites More sharing options...
cmgmyr Posted March 18, 2007 Share Posted March 18, 2007 no problem, glad to help Quote Link to comment https://forums.phpfreaks.com/topic/43259-quick-noob-question/#findComment-210105 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.