jmr3460 Posted June 1, 2009 Share Posted June 1, 2009 What is wrong with this code: <?php if(setcookie("pam","Pamela is great")); { echo "The deal is done!";} else {echo "No cookie for you!"; } ?> I get this error: Parse error: syntax error, unexpected T_ELSE in /home3/simplic5/public_html/php/index.php on line 5 Quote Link to comment https://forums.phpfreaks.com/topic/160429-solved-if-and-else/ Share on other sites More sharing options...
DarkSuperHero Posted June 1, 2009 Share Posted June 1, 2009 <?php if(setcookie("pam","Pamela is great")) //you did not need a ; at the end of this line, that was causing an error { echo "The deal is done!"; } else { echo "No cookie for you!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160429-solved-if-and-else/#findComment-846612 Share on other sites More sharing options...
jmr3460 Posted June 1, 2009 Author Share Posted June 1, 2009 I need to read more. Is that not the end of that statement? By the way that fixed it. Quote Link to comment https://forums.phpfreaks.com/topic/160429-solved-if-and-else/#findComment-846614 Share on other sites More sharing options...
.josh Posted June 1, 2009 Share Posted June 1, 2009 conditions are not terminated by semicolons. Conditions use { } brackets to mark the beginning and end of what you want executed if they evaluate true. But, if you are only wanting to execute one expression if it evaluates true, you don't need the { } brackets. Examples: if (condition) expression1; // will be executed if condition is true if (condition) { expression1; // will be executed if condition is true, same as first one } if (condition) expression1; // will be executed if condition is true expression2; // will be executed regardless of whether condition is true or not, as it is not tied to the condition if (condition) { expression1; // will be executed if condition is true expression2; // will be also only be executed if condition is true, since it is wrapped inside the brackets } So technically, you could have written your code like this: <?php if(setcookie("pam","Pamela is great")) echo "The deal is done!"; else echo "No cookie for you!"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/160429-solved-if-and-else/#findComment-846617 Share on other sites More sharing options...
jmr3460 Posted June 1, 2009 Author Share Posted June 1, 2009 Thanks I have some homework to do. Learning the definitions and terms and the syntax for them. I was just looking for some books on these subjects. Thanks again for the replys! Quote Link to comment https://forums.phpfreaks.com/topic/160429-solved-if-and-else/#findComment-846620 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.