PHP question

Discussion in 'PHP' started by Jim bob 9 pants, Nov 6, 2006.

  1. #1
    Hi

    Can this be done?? and if so how

    I want some text to show unless 0 shows. I dont mean a zero result from the data base.

    In context

    eg

    Brian has 4 (i want to show)

    Brian has 0 (i don't want to show)

    4 and 0 comes from a data base.

    Really any number greater than 0 I want to show!!

    Hope someone can help - if this makes sense

    Regards
     
    Jim bob 9 pants, Nov 6, 2006 IP
  2. RJP

    RJP Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    
    if ($number > 0){
    print 'Brian has '.$number;
    }
    
    
    PHP:
    So you are only displaying (printing) the text if the number variable is greater than zero.

    -Ryan
     
    RJP, Nov 6, 2006 IP
  3. Jim bob 9 pants

    Jim bob 9 pants Peon

    Messages:
    890
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I love you
     
    Jim bob 9 pants, Nov 6, 2006 IP
  4. master06

    master06 Peon

    Messages:
    2,806
    Likes Received:
    121
    Best Answers:
    0
    Trophy Points:
    0
    #4
    or

    
    if($number > 0)
    {
       echo 'Brian has'.$number;
    }
    else
    {
    } 
    
    PHP:
    Regards
     
    master06, Nov 6, 2006 IP
  5. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You could also do the following

    if($number == 0){
        echo "Brian doesn't have any apples";
    } elseif($number == 1){
        echo "Brian has one apple";
    } else {
        echo "Brian has" . $number . "apples";
    }
    PHP:
     
    streety, Nov 6, 2006 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Or

    
    
    if ($number == 0)
    {
        echo "Brian doesn't have any apples";
    }
    else
    {
        echo "Brian has ". $number ." apple". ($number != 1 ? 's' : '') .'.';
    }
    
    PHP:
     
    nico_swd, Nov 6, 2006 IP
  7. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #7
    Alternatively, you might want to use the following:

    Forget it, there are already too many above... ;)
    Just kidding.
    Bye :)
     
    JEET, Nov 6, 2006 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Wait, wait, or maybe this. :p

    
    switch($number)
    {
          case 0:
             echo "Brian doesn't have any apples";
             break;
      
          case 1:
             echo "Brian has one apple";
             break;
      
          default:
             echo "Brian has" . $number . "apples";
    }
    
    
    Code (markup):
     
    nico_swd, Nov 6, 2006 IP
  9. Jim bob 9 pants

    Jim bob 9 pants Peon

    Messages:
    890
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #9
    What a great selection of replies.

    Thing is Brian hates F..king apples now and wants banana's

    Jamie
     
    Jim bob 9 pants, Nov 6, 2006 IP
  10. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Brian will have what he is given and be grateful for it! :D
     
    streety, Nov 6, 2006 IP