Jump to content

Code Not Displaying Properly ?


cdm89

Recommended Posts

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")};

?>

Link to comment
Share on other sites

<?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 by premiso
Link to comment
Share on other sites

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. 

Link to comment
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.