Simple Simple PHP If Statement Help Please!

Discussion in 'PHP' started by wd_2k6, Jul 18, 2008.

  1. #1
    OK, I have:

    <?php if($dabadee !== '') { echo $dabadee; } else { echo da_badoo(); } ?>

    This is correct, however i want to change the second part so that if dabadee is not there it will output some string e.g "hello" ?

    At the moment it will output da_badoo();, but i want to change this to some string but can't get it to work? So instead of da_badoo(); it should output the word "hello"
     
    wd_2k6, Jul 18, 2008 IP
  2. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    
    <?php if($dabadee!='' )//diff
    {
    echo $dabadee;
    }
    else
    {
    echo "Hello";//diff
    }
    ?>
    
    PHP:
    '//diff' indiciates the lines that have been changed.
    If the string you want to output contains a variable that should be parsed, wrap your string in double quotes, else if use single quotes.
     
    rohan_shenoy, Jul 18, 2008 IP
    wd_2k6 likes this.
  3. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thankyou, worked great especially with your detailed explanation, again thanks very much!!
     
    wd_2k6, Jul 18, 2008 IP
  4. Mozzart

    Mozzart Peon

    Messages:
    189
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Also, use this
    
    <?php if(!empty($dabadee))//diff
    {
    echo $dabadee;
    }
    else
    {
    echo "Hello";//diff
    }
    ?>
    
    PHP:

    http://php.net/empty will probably be easier for you to use than typing $var != '' or $var == '' etc etc. Save some typing I guess :)
     
    Mozzart, Jul 18, 2008 IP
  5. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #5
    ^empty() will treat 0 as empty too.
     
    rohan_shenoy, Jul 19, 2008 IP