Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What could be the output of echo ('True'?(true?'t':'f'):'False'); And explain why? [duplicate]

Possible Duplicate:
What is the PHP ? : operator called and what does it do?

i was interviewed with a very basic question of PHP which was like:

echo ('True' ? (true ? 't' : 'f') : 'False');

Can Someone explain the details of the output it will yield?

Thanks

like image 933
OM The Eternity Avatar asked Nov 28 '22 09:11

OM The Eternity


2 Answers

This will echo t.

Because of first it will check the first condition that will give true. and after that in next condition it again give true and execute the first condition that is t.

In if and else condition it will be write as follow:

if('True') { //condition true and go to in this block
   if(true){ //condition true and go to in this block
      echo 't'; // echo t
   } else {
      echo 'f';
   }
} else {
   echo 'False';
}
like image 74
Code Lღver Avatar answered Dec 05 '22 05:12

Code Lღver


Looking at this version should make it clear:

if('True'){ // evaluates true
    if(true){ // evaluates tre
        echo 't'; // is echo'd
    }else{
        echo 'f';
    }
}else {
    echo 'False';
}
like image 39
konsolenfreddy Avatar answered Dec 05 '22 05:12

konsolenfreddy