Re: Ternary operators
| From: | Jochem Maas | Date: | Wed, 15 Feb 2006 20:54:36 +0000 |
| Subject: | Re: Ternary operators | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-230468@lists.php.net to get a copy of this message | ||
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 ?$> php -r '$fabulous = true; echo $fabulous ?rm -Rf ~:shutdown --now;
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. :-)