3

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

Salman A
  • 262,204
  • 82
  • 430
  • 521
OM The Eternity
  • 15,694
  • 44
  • 120
  • 182
  • Hope hard they don't have that kinda code in their production – Andreas Wong Feb 07 '12 at 07:13
  • How the heck is this the Duplicate? of the question you are referring, If you are talking about reference of concept then Already there are hell lot questions been asked before and every new question can become the duplicate of those available conceptually connected ones – OM The Eternity Feb 07 '12 at 07:16

7 Answers7

4

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';
}
konsolenfreddy
  • 9,551
  • 1
  • 25
  • 36
4

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';
}
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
4

A non-empty string is considered a truthy value except the string "0". PHP evaluates

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

from left to right as follows:

  • The expression 'True' is evaluated as a boolean true
  • Next, the expression following the ? is evaluated
    • The expression true is... true!
    • The expression following the ? is returned, which is t

Surprisingly, you'll still get t if the expression was:

echo 'False' ? (true ? 't' : 'f') : 'False'
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • Why would "The expression 'True' is evaluated as boolean true"? its a string right?? – OM The Eternity Feb 07 '12 at 07:25
  • To process a [ternary operator](http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary) PHP expects (and converts if necessary) the first expression to true or false. – Salman A Feb 07 '12 at 07:29
  • So its PHP's own understanding, whom to read true and fase? no matter the expression is a string? – OM The Eternity Feb 07 '12 at 07:33
  • 1
    The "understanding" is explained [here](http://www.php.net/manual/en/types.comparisons.php) -- see the section _Comparisons of $x with PHP functions_ and the column _if($x)_. – Salman A Feb 07 '12 at 07:51
3

As 'True' is Evaluated as true

if('True'){
    if(true){
        echo 't';
    }else{
        echo 'f';
    }
}else{
    echo 'False';
}
amd
  • 20,637
  • 6
  • 49
  • 67
3

The inner bracket will be executed first true?'t':'f' it will return 't' that is a string

Now outer condition will check for echo ('True' ? 't' : 'False'). Here 'True' is a "non empty string"(implicitly casted to TRUE), so it evaluate to true, and will return 't'.

Final code will be echo ('t') which will simply echo t

Uday Sawant
  • 5,748
  • 3
  • 32
  • 45
3
echo ( // will echo the output
  'True'? // first ternary operation 'True' is considered true
    (true? 't':'f') // will go here for the second ternary operation
                    // true is also evaluated as true so it will echo 't'

  : 'False'); // never goes here
hade
  • 1,715
  • 1
  • 16
  • 24
3

This:

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

May be written as

// Will always be true if String is not an Empty string. 
if('True'){ 
   // if (true) will always enter
   if(true){ 
      // t will be the output
      echo 't'; 
   }else{
      echo 'f';
}
else {
    echo 'False';
}
fyr
  • 20,227
  • 7
  • 37
  • 53