SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    VICTORY IS MINE! luchazard's Avatar
    Join Date
    May 2001
    Location
    USA
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    My first ever PHP script has a problem...

    Well I guess its expected, so I can't feel to bad

    Anyway here is what I did:

    PHP Code:
    <?

    // if $name doesn't exist, display the first page

    if (!$name)

    {

    ?>

    <html>
    <form method="POST" action="login.php">
    So what's your name?
    <input type="text" name="name" size="10">
    <input type="submit" name="submit">
    </form>
    </html>

    <?

    }
    else
    {

    // if $name does exist, the form has been submitted

    switch ($name)

    {

    if (
    $name == "Luke")

    {  
    echo(
    "<center><b>Welcome Luke!</center></b>");
    }

    elseif (
    $name == "Neo")

    {
    echo(
    "<center><b>Welcome to the Matrix!</center></b>");
    }

    else
        
    {
    echo(
    "$name - Who are you?");
    }

    }

    }

    ?>
    And I got this error:

    Parse error: parse error, expecting `T_CASE' or `T_DEFAULT' or `'}'' in /home/luchazard/www/login.php on line 31

    Anyway can anyone tell me what I did wrong? Thanks!
    Last edited by luchazard; Sep 3, 2001 at 19:18.

  2. #2
    VICTORY IS MINE! luchazard's Avatar
    Join Date
    May 2001
    Location
    USA
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or even better... if you know a 'neater' way to write the code, that'd be awsome. Its my first script so its a little bulky I'd persume.

  3. #3
    VICTORY IS MINE! luchazard's Avatar
    Join Date
    May 2001
    Location
    USA
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *bump - anyone???

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Location
    North East, England
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    I am a newby myself so this may be a case of the blind leading the blind but since no one has answered I will give you my 2 pence worth.

    The problem is with your switch statement. This is how it should read.

    PHP Code:
    <?

    // if $name doesn't exist, display the first page

    if (!$name)

    {

    ?>

    <html>
    <form method="POST" action="login.php">
    So what's your name?
    <input type="text" name="name" size="10">
    <input type="submit" name="submit">
    </form>
    </html>

    <?

    }
    else
    {

    // if $name does exist, the form has been submitted

    switch ($name)

    {

    case
    "Luke":
    echo(
    "<center><b>Welcome Luke!</center></b>");
    break;

    case
    "Neo":
    echo(
    "<center><b>Welcome to the Matrix!</center></b>");
    break;

    default:
    echo(
    "$name - Who are you?");

    }

    }

    ?>
    If you use a switch statement you do not use if and else clauses. You just do it like above. In this code the first case:

    If Luke is the name then this case evaluates to true and 'Welcome Luke' is printed out. The break statement ends execution because this is the right name.

    If the name was not Luke then the next statement would be evaluated. If this was false it would move to the next one until a case is evaluated true or until the default statement is reached. If the default statement is reached none of the above names were used and the default expresion is executed. i.e. '$name - who are you' is printed.

    I hope this helps and I am prety sure it is right
    Lee

  5. #5
    SitePoint Addict
    Join Date
    Apr 2001
    Location
    None
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <? if (!$name) { ?>
    <form method="POST" action="login.php">
    So what's your name?
    <input type="text" name="name" size="10">
    <input type="submit" name="submit">
    </form>
    <?
    }
    if ((
    $submit) && ($name)) {
    echo 
    "Welcome $name";
    }
    ?>
    Last edited by Pweb; Sep 3, 2001 at 20:13.

  6. #6
    VICTORY IS MINE! luchazard's Avatar
    Join Date
    May 2001
    Location
    USA
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright,

    Thanks Michael! So as a general rule of themb: Never use switch statements with if and else clauses.

    Also does anyone know if even your not using a switch statement... should I use if and else clauses or 'case and break' stuff (I know its not called case and break... what is the right name)

    Anyway thanks!!

  7. #7
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The case and break are the switch statement, the if, elseif and else are a separate entity.

    For more information between the two, check out this thread:
    http://www.sitepointforums.com/showt...threadid=31456
    Oh no! the coots are eating my nodes!

  8. #8
    VICTORY IS MINE! luchazard's Avatar
    Join Date
    May 2001
    Location
    USA
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cool, thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •