Question about if statement

Discussion in 'PHP' started by mirmigis, Sep 30, 2009.

  1. #1
    Perhaps its an obvious question, but really:

    Is
    if(somthing){ //do things
    }
    PHP:
    equal to
    if(somthing==true){ //do things
    }
    PHP:
    ?
     
    mirmigis, Sep 30, 2009 IP
  2. young coder

    young coder Peon

    Messages:
    302
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    maybe .................

    :D
     
    young coder, Sep 30, 2009 IP
  3. mirmigis

    mirmigis Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ok ok....... :p
     
    mirmigis, Sep 30, 2009 IP
  4. sudeep333

    sudeep333 Peon

    Messages:
    321
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    if "something" is returning true then only the block will gets executed.

    for example:

    <?php
    something = false;

    if(somthing){ //do things
    }
    ?>

    Then the block will not gets executed

    The condition "something" must return true to execute the code.

    in same is true for the second example.
     
    sudeep333, Oct 1, 2009 IP
  5. pixmania

    pixmania Peon

    Messages:
    229
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    function something() { 
        $something = "?????"; // get the something
        if (empty($something)) {
        $something = $something_else; // possible default
        }
     return $something;
    }
    
    PHP:
     
    pixmania, Oct 1, 2009 IP
  6. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Wow, I swear the people responding are trying to confuse you. Here's the answer you are looking for:

    yes
     
    plog, Oct 1, 2009 IP
  7. mirmigis

    mirmigis Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    :D:D:D

    Damn! I knew it!!!
     
    mirmigis, Oct 1, 2009 IP
  8. pixmania

    pixmania Peon

    Messages:
    229
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    hmm but maybe something = nothing ? then what if = something
     
    pixmania, Oct 1, 2009 IP
  9. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #9
    
    
    if(false) {
    // wont get executed
    }
    if(null) {
    // wont get executed
    } 
    if(0) {
    // wont get executed
    } 
    if("false") {
    // will get executed
    } 
    if("null") {
    // will get executed 
    }
    if("string"==true) {
    // will get executed
    }
    if("false"==false) {
    // wont get executed
    }
    if("false"==true) {
    // will get executed
    }
    if("true"==false) {
    // wont get executed
    }
    if("true"==true) {
    // will get executed
    }
    
    PHP:
    And soooooo on (cases with 0, "0", null and "null").
    It can get pretty confusing =)
     
    Gray Fox, Oct 1, 2009 IP
  10. pixmania

    pixmania Peon

    Messages:
    229
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    its a php situation and better defined if we knew the if =

    You cant just say it equals 1 or 0, true or false // thats a result
     
    pixmania, Oct 1, 2009 IP
  11. mirmigis

    mirmigis Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11


    check this http://www.blueshoes.org/en/developer/php_cheat_sheet
     
    mirmigis, Oct 2, 2009 IP
  12. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #12
    Is an elephant a house?

    Well, what kind of elephant--asian or african? Do you consider an apartment a house? This isn't a toy elephant is it? Do you want to paint the house or get aluminum siding? Do you know how much elephants eat in a day?

    You are all missing his question. He doesn't care about specific values of the variable something. He wants to know if those two conditional tests are equivalent.
     
    plog, Oct 2, 2009 IP
  13. frantech

    frantech Greenhorn

    Messages:
    96
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    20
    #13
    Hey there,

    Which still is a 'maybe', because we don't know what 'something' is.

    I guess if he had something like

    $something = false;

    if($something == true) {
    // i will fail
    }

    then what he's asking should stand correct, assuming $something is a defined variable.

    Granted, i'm not sure why you would do that :)

    Odd question either way!

    Francisco
     
    frantech, Oct 2, 2009 IP