PHP code

Discussion in 'PHP' started by roice, Jan 5, 2011.

  1. #1
    Hello, I bump into this 2 codes and I didn't understand what they are doing and for what use can (or should I use) this type or php writing...?

    1.
    $x = 100 == '100' ? 'yes' : 'no' ; 
    echo $x;
    PHP:

    2.
    $x = 100 === '100' ? 'yes' : 'no' ; 
    echo $x;
    PHP:
    Please try to explain it to me in the abstractest way you can.
    Thank you in advance.
     
    roice, Jan 5, 2011 IP
  2. Cida

    Cida Peon

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It's called Ternary Operation
    
    $x = 100 === '100' ? 'yes' : 'no'; 
    
    Code (php):
    is they equivalent of
    
    if(100 === '100')
    {
       $x = 'yes';
    }
    else
    {
       $x = 'no';
    }
    
    Code (php):
    condition ? true : false;

    Because the identical === operator is used, data type will be taken into the comparison, resulting in $x having the string value of "no".

    Frustrating I can't post links properly yet.
    php .net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
     
    Last edited: Jan 5, 2011
    Cida, Jan 5, 2011 IP
  3. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks!

    I didn't got you on the '===' thing
    I understand that its check if its equal and have the same type. but who it check the type?
     
    roice, Jan 5, 2011 IP
  4. Cida

    Cida Peon

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    == is the equals operator, and === is the identical operator.
    e.g
    
    if('1' === 1)
    {
       echo '1';
    }
    elseif('1' == 1)
    {
       echo '2';
    }
    
    Code (php):
    That should output 2, '1' is a string and 1 is an integer, making them not identical, though they are equal.
     
    Cida, Jan 5, 2011 IP
  5. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    roice, Jan 5, 2011 IP
  6. Cida

    Cida Peon

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Of course two identical values are also equal, it goes without saying.

    The difference between "else" and "elseif" is that elseif has an extra condition, else will execute if the above if/elseif evaluated false, elseif will execute if the above if/elseif is false, and the condition set is also true; else must also be last, you can't follow an elseif after an else.
     
    Cida, Jan 6, 2011 IP
  7. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    so "elseif" it's like writting :
    else
    {
    if {}
    }


    ?
     
    roice, Jan 9, 2011 IP
  8. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #8
    Pretty much,

    
    $a=1;
    if($a==2)
    {
     echo "foo";
    }
    else
    {
     echo "bar";
    }
    //Echoes bar
    if($a==2)
    {
    echo "foo!";
    } elseif($a==3){
    echo "bar!";
    } elseif($a==1){
     echo "foobar!";
    }
    //echoes foorbar!
    
    PHP:
     
    ssmm987, Jan 9, 2011 IP
  9. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    OK, thanks...
     
    roice, Jan 9, 2011 IP