Whats wrong with my code?

Discussion in 'PHP' started by zambonibutters, Aug 9, 2010.

  1. #1
    Okay, here's the code:

    <?php
    $x=1;
    while($x<5);
    {
    echo "hello";
    $x++;
    }

    ?>
    It's quite basic, but after ages of troubleshooting, it's just not working! Can anybody find what the problem is here?
    I'm a noob, so be kind.
    Thanks soo much!
     
    zambonibutters, Aug 9, 2010 IP
  2. Mindraven

    Mindraven Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It's the semicolon after your while line. So it's stuck right there and looping since it's never going through your loop. Code should be:

    
    <?php
    $x=1;
    while($x<5)
    {
    echo "hello";
    $x++;
    }
    
    ?>
    
    PHP:
     
    Mindraven, Aug 9, 2010 IP
  3. zambonibutters

    zambonibutters Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks. I came across another problem though:
    <?php
    $x=1;
    $y="hello ";
    while($x<2)
    {
    echo $y;
    $x++;
    }
    if (strlen ($y)==2);
    echo "goodbye";
    ?>
    The problem here is that it's still saying goodbye, even though hello has more than 2 characters.
     
    zambonibutters, Aug 9, 2010 IP
  4. Mindraven

    Mindraven Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you only want goodbye to display if $y is 2 characters long then you need to put it in a clause

    
    <?php
    $x=1;
    $y="hello ";
    while($x<2)
    {
    echo $y;
    $x++;
    }
    if (strlen ($y)==2) {
    echo "goodbye";
    }
    ?>
    
    PHP:
     
    Mindraven, Aug 9, 2010 IP
  5. zambonibutters

    zambonibutters Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you so much. I keep getting hung up on the sytax. Is there anything I could download where I can be shown what's wrong before I save it and test it? Thanks again for the help.
     
    zambonibutters, Aug 9, 2010 IP
  6. AntelopeSalad

    AntelopeSalad Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Well if you want a full blown IDE for PHP and other web/non-web languages I strongly recommend Netbeans. Out of every free heavy duty IDE I've tried it seems to be the best.

    It will tell you if something is wrong with your syntax (in your case, the extra semi-colons), help you auto-complete method/etc. names, has awesome documentation for the languages you'll be working with, and continuously adds support for popular frameworks of languages (jQuery for example).

    I'm not going to parrot all of its features, just goto http://www.netbeans.com/
     
    AntelopeSalad, Aug 9, 2010 IP
  7. zambonibutters

    zambonibutters Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thank you all
     
    zambonibutters, Aug 9, 2010 IP
  8. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #8
    Use dreamweaver CS5, go to code view options then onto highlight invalid code, it will show you an error if there is one and highlight in yellow.

    Glen
     
    HuggyEssex, Aug 9, 2010 IP
  9. andymoo

    andymoo Peon

    Messages:
    169
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    ++$x is quicker than $x++
     
    andymoo, Aug 10, 2010 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    It's insignificant for a small amount of loops. It also changes the behavior in a for() loop.

    The real significant difference is that the former increases, and THEN returns the value, while the latter returns, and then increases.
     
    nico_swd, Aug 10, 2010 IP