2

In the javascript world we can run a function through a Ternary comparison, I wanted to see if this applies to PHP, it seems it does only to an extent.

This is not for production use nor will it be actually used. This is merely a topic to see PHP's in-depth extent of the ternary comparison.

<?
 $a = 1;
 $b = 1;

 function doThis($a){
     print "$a";
 }

 $a == $b ? ( doThis('TRUE') ):( print "FALSE" );
?>

The above code works perfectly, however, is it possible to run multiple functions and or operations within ()?

Such as?

 $a == $b ? ( doThis('TRUE'), doThis('THAT') ):( print "FALSE" );

or even?

$a == $b ? ( function(){ print "33"; doThis("TRUE") } ):( print "FALSE" );
Michael Mikhjian
  • 2,760
  • 4
  • 36
  • 51
  • 1
    Did you try it? You'll find the immediate answer to be *no* for your specific examples. – Jason McCreary Jun 19 '13 at 17:31
  • 2
    But bear in mind that doing so will make your code that much harder to read, and therefore to maintain. It might look quick now, but six months from now.... – andrewsi Jun 19 '13 at 17:32
  • @JasonMcCreary of course, my methods did not work. I'm wondering if there is another way to deploy this. – Michael Mikhjian Jun 19 '13 at 17:32
  • @andrewsi oh absolutely, I won't be applying this concept to my code, I'm must curious to see how far indepth PHP can handle something like this. This is also useful for obfuscating or potentially performance boost. – Michael Mikhjian Jun 19 '13 at 17:33
  • @Joe yes, that's the obvious way to do it. Like I said, this is just out of curiosity. – Michael Mikhjian Jun 19 '13 at 17:39
  • -1 for blatant abuse of `? :`. It should not often replace `if`. It's for when you have two *expressions* you're deciding between, and you want the *value* of exactly one of those expressions. – cHao Jun 19 '13 at 17:43
  • @cHao re-read my topic before handing off -1's. – Michael Mikhjian Jun 19 '13 at 18:00
  • @MichaelMikhjian: The only thing worth knowing in this case is: `?`/`:` is purely for expressions (ie: values). Period. Ideally, the expressions wouldn't even have side effects, but at the very least, their values are what it decides between. Never use it just for side effects. Ever. There is literally *no* good reason to do it, and at least a dozen reasons not to. – cHao Jun 19 '13 at 18:57
  • @cHao And I'm not saying I don't agree with you; I agree with you 100%. This is simply a question: Can PHP do it, and if so how? – Michael Mikhjian Jun 19 '13 at 19:19
  • @MichaelMikhjian: And if we agree 100% that this should never be done, then the question of whether it can be done is irrelevant -- and the answer to the question of *how* to do it makes things worse. – cHao Jun 19 '13 at 19:37

1 Answers1

2

You can have the ternary return a closure that would perform the requested function

 $func = $a==$b?function(){ print "33"; doThis("TRUE"); }:function(){ print "FALSE"}); );
 $func();

or borrowing from javascript you can create a IIFE (Immediately Invoked Function Expression)

 $a==$b?call_user_func(function(){print "33"; doThis("TRUE");}):
        call_user_func(function(){print "FALSE"; });
Orangepill
  • 24,500
  • 3
  • 42
  • 63