Ternary conditionals

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
armsys
Posts: 557
Joined: 10 Mar 2012 12:40
Location: Hong Kong

Ternary conditionals

Post by armsys »

Referring to

Code: Select all

::$r = ("a" == "a"); $r = ($r?"True":"False"); msg $r;
,
how to interpret ($r?"true":"False")?
Is the "?" (question mark) an implied "if"? :oops:

highend
Posts: 13315
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Ternary conditionals

Post by highend »

It's a ternary conditional.

Advanced Topcis - Scripting - Ternary Conditionals

Code: Select all

$r = ("a" == "a");
$r = 1

Code: Select all

$r = ($r?"True":"False");
Because $r = 1 (which is also a boolean "true") -> $r = "True"

It's just a short form of an if / else statement.
One of my scripts helped you out? Please donate via Paypal

armsys
Posts: 557
Joined: 10 Mar 2012 12:40
Location: Hong Kong

Re: Ternary conditionals

Post by armsys »

highend wrote:It's a ternary conditional.
Yes, I found it in the manual. I should have read ahead to locate the description on the weird construct (condition)? value-if-true : value-if-false.
Thank you for your fast help.

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Ternary conditionals

Post by bdeshi »

in my opinion, it's a good idea to enclose the whole thing in another set of braces: ( (condition) ? ReturnIfTrue : ReturnIfFalse )
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

armsys
Posts: 557
Joined: 10 Mar 2012 12:40
Location: Hong Kong

Re: Ternary conditionals

Post by armsys »

SammaySarkar wrote:in my opinion, it's a good idea to enclose the whole thing in another set of braces: ( (condition) ? ReturnIfTrue : ReturnIfFalse )
Thank you for your reminder. Agree. I'll follow your scripting style because it helps improve readability.

autocart
Posts: 1248
Joined: 26 Sep 2013 15:22

Re: Ternary conditionals

Post by autocart »

1) is it safe to use expressions in the if-true and if-false parts of the ternary conditional?
2) is it safe to nest ternary conditionals? e.g.: $coolVar = $groofyVar?groofyVar:isset($coolVar)?$coolVar:""; $coolVar = $groovyVar?groovyVar:isset($coolVar)?$coolVar:"";
Last edited by autocart on 15 Sep 2016 12:14, edited 1 time in total.

admin
Site Admin
Posts: 60566
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Ternary conditionals

Post by admin »

I'd think so. But the parsing can get tricky depending on what you do. Make a test...

ADD:
Parentheses help. This seems to work. Play with it...

Code: Select all

// nested ternaries
  //$coolVar = "cool";
  //$groofyVar = "groof";
  echo (isset($groofyVar)?$groofyVar:(isset($coolVar)?$coolVar:"NO"));

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Ternary conditionals

Post by bdeshi »

It's perfectly okay to nest ternary conditionals or expressions within another ternary. You can also have ternaries in expressions. As I commented earlier here, when in doubt, enclose in braces.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

autocart
Posts: 1248
Joined: 26 Sep 2013 15:22

Re: Ternary conditionals

Post by autocart »

just realized that groovy is spelled with a "v". (I think I was connecting it to goofy in my mind at first.)
sorry for misleading u, Don, I am also not a native English speaker.

Thx for all help.

admin
Site Admin
Posts: 60566
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Ternary conditionals

Post by admin »

Luckily I coded it so that mispelled variables also work! :titter:

Post Reply