Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: simple question

  1. #1
    New Coder
    Join Date
    Jul 2003
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question simple question

    Just begun to learn PHP.
    I never did C before, though to underswtand bsics i need to dig into it step by step.

    Just wonder how to correct this snippet:

    PHP Code:
    if ($age>20)     {
        echo 
    "You are older than 20 years old.";
            } else
            {
        echo 
    "You are younger than 20 years old.";
            } 
    I mean if I enter 20 in form, script outputs: You are younger than 20 years old. Why does it behave such way? I need it to say: You are 20. How to make it?

    TNX!

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It behaves like that because 20 = 20 and your condition 20 > 20 will return false.

    So you could do something like
    PHP Code:
    if ($age == 20) {
        echo 
    "You are 20 years old.";
    } elseif (
    $age20) {
         echo 
    "You are older than 20 years old.";
    } elseif (
    $age 20) {
         echo 
    "You are younger than 20 years old.";

    Or you could use a switch() http://be2.php.net/manual/en/control...res.switch.php

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    or, if you wanted to do a single line:

    PHP Code:

    $return 
    = ($age 20) ? "You are older than 20" : ($age == 20) ? "You are 20" "You are younger than 20";
    echo 
    $return
    this does basically what was put above, but in a single line.

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Originally posted by missing-score
    or, if you wanted to do a single line:

    PHP Code:

    $return 
    = ($age 20) ? "You are older than 20" : ($age == 20) ? "You are 20" "You are younger than 20";
    echo 
    $return
    this does basically what was put above, but in a single line.
    The technical name for doing so is using the ternary operator. The ternary operator being the ? character.

    Simply put, a shortcut way of writing a conditional if/else block. It's use is typically discouraged though due to poor readability and in some people's cases writability.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks.. never knew it was called that.

  • #6
    New Coder
    Join Date
    Jul 2003
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh...thank all of you, lads!


  •  

    Posting Permissions

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