Re: Ternary operators

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Carl Furst wrote:
Hey all,

Question about ternary operators. You can't really use functions INSIDE
ternary operators, right? They can only be tokens and operators?

So

$fabulous = true;

$fabulous ? echo "I am fabulous!\n" : echo "I am a looser!\n";
Would not work?

testing this is quite easy (given your reference to backtick syntax):


$> php -r '$fabulous = true;$fabulous ? echo "I am fabulous!\n" : echo "I am a looser!\n";'

Parse error: parse error, unexpected T_ECHO in Command line code on line 1



And this:

echo  $fabulous ? "I am fabulous!\n" : "I am a looser!\n";

would?


$> php -r '$fabulous = true; echo $fabulous ? "I am fabulous!\n" : "I am a looser!\n";'
I am fabulous!



Also if operators are used you can you use the backtick to do some nasty
nasty system stuff like:

$fabulous ? `rm -Rf ~` : `shutdown --now`;


$> php -r '$fabulous = true; echo $fabulous ? `echo TOOL` : `echo tool`;'
TOOL


So would it ever be contrary to allow the ternary operator to do different
functions on different evaluations of the Boolean Expression at the
beginning; like in the first example?

yes and no. echo is not a function, it's a language construct.
you can do stuff like:

$> php -r '
function func1() { echo "TRUE\n"; }
function func2() { echo "FALSE\n"; }

$f = true; $f ? func1(): func2();
'
TRUE

please say your going start using "php -r" to test stuff like this. :-)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux