Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/43259-quick-noob-question/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/43259-quick-noob-question/#findComment-210036
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/43259-quick-noob-question/#findComment-210042
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/43259-quick-noob-question/#findComment-210044
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/43259-quick-noob-question/#findComment-210049
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.