cdm89 Posted November 26, 2012 Share Posted November 26, 2012 Hey everyone, sorry this is really simple but i am a big nube! so my class assignment is to create a script that counts to ten and displays which numbers are odd and which numbers are even. I'm almost positive that my code is correct, however the browser just displays my last bit of code starting at the '; to the end of the if/else statement. ...can someone please take a look at this ive been staring at it for days lol i think the problem is incorrect syntax, or maybe i didnt close something somewhere? but from what i can see i have all my closing tags ? <php for($x = 0; $x < 10; $x++) { echo 'Iteration: ' . '$x' . '<br />'; } if ($x & 1) {echo ("odd")}; else {echo ("even")}; ?> Quote Link to comment https://forums.phpfreaks.com/topic/271204-code-not-displaying-properly/ Share on other sites More sharing options...
premiso Posted November 26, 2012 Share Posted November 26, 2012 You need to move the if statement inside of the for loop. Quote Link to comment https://forums.phpfreaks.com/topic/271204-code-not-displaying-properly/#findComment-1395243 Share on other sites More sharing options...
cdm89 Posted November 26, 2012 Author Share Posted November 26, 2012 You need to move the if statement inside of the for loop. Thanks for the help, unfortunately this didn't seem to work now the browser just displays '; if ($x & 1) {echo ("odd")}; else {echo ("even")}; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/271204-code-not-displaying-properly/#findComment-1395247 Share on other sites More sharing options...
premiso Posted November 26, 2012 Share Posted November 26, 2012 (edited) <?php for($x = 0; $x < 10; $x++) { echo 'Iteration: ' . $x . '<br />'; if ($x & 1) { echo ("odd"); }else { echo ("even"); } } ?> The browser just displayed that as you forgot the ? in the <?php, I fixed the code above, if you have questions about it let me know and I or someone else will be glad to assist. Edited November 26, 2012 by premiso Quote Link to comment https://forums.phpfreaks.com/topic/271204-code-not-displaying-properly/#findComment-1395248 Share on other sites More sharing options...
cdm89 Posted November 26, 2012 Author Share Posted November 26, 2012 Ah, thanks! The code is working now. The only problem is, instead of displaying numbers it just displays my variable $x ten times Quote Link to comment https://forums.phpfreaks.com/topic/271204-code-not-displaying-properly/#findComment-1395255 Share on other sites More sharing options...
premiso Posted November 26, 2012 Share Posted November 26, 2012 Look closer at the code I posted. Remove the single quotes ( ' ) around the $x part, as I did and it should display the numbers. When variables are used inside of single quotes, it takes it literally and displays $x, if you remove them or change them to Double Quotes (which double quotes would parse the variable) it would / will display the number. Quote Link to comment https://forums.phpfreaks.com/topic/271204-code-not-displaying-properly/#findComment-1395257 Share on other sites More sharing options...
cdm89 Posted November 26, 2012 Author Share Posted November 26, 2012 Thank you so much, simple and dumb mistakes i need to pay more attention to detail.... Quote Link to comment https://forums.phpfreaks.com/topic/271204-code-not-displaying-properly/#findComment-1395263 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.