Aureole Posted October 11, 2007 Share Posted October 11, 2007 <?php if(... ? ... : ...) { // Do stuff... } ?> I think this is called a ternary operator, I've read about it on a few sites including the PHP manual but I am still confused as to how it works... can someone please explain rather elaborately... as I just don't get it. Quote Link to comment https://forums.phpfreaks.com/topic/72818-solved-ternary-operator/ Share on other sites More sharing options...
~n[EO]n~ Posted October 11, 2007 Share Posted October 11, 2007 There is a great definition and example in the PHP Manual in 14. Expressions I pasted lil' bit from there :: There is one more expression that may seem odd if you haven't seen it in other languages, the ternary conditional operator: <?php $first ? $second : $third ?> If the value of the first subexpression is TRUE (non-zero), then the second subexpression is evaluated, and that is the result of the conditional expression. Otherwise, the third subexpression is evaluated, and that is the value. Quote Link to comment https://forums.phpfreaks.com/topic/72818-solved-ternary-operator/#findComment-367239 Share on other sites More sharing options...
wildteen88 Posted October 11, 2007 Share Posted October 11, 2007 The ternary operator is just an if else statement, without the words if/else, instead they are symbols instead. If you understand an if/else statement you'll be able to use a ternary operator. The syntax of a ternary operator is this: (condition) ? true : false The above is the same as: if(condition) { true } else { false } Ternary operators are inline, they cannot execute blocks of code. Quote Link to comment https://forums.phpfreaks.com/topic/72818-solved-ternary-operator/#findComment-367244 Share on other sites More sharing options...
Aureole Posted October 11, 2007 Author Share Posted October 11, 2007 I think I get it now. So is this... <?php if($_SESSION['logged_in'] == 1) { $this->output = 1; } else { $this->output = 0; } ?> ...the same as this... <?php if($_SESSION['logged_in'] == 1) ? $this->output = 1 : $this->output = 0; ?> ...? Also is that the correct syntax? Quote Link to comment https://forums.phpfreaks.com/topic/72818-solved-ternary-operator/#findComment-367261 Share on other sites More sharing options...
Orio Posted October 11, 2007 Share Posted October 11, 2007 Drop the if: <?php ($_SESSION['logged_in'] == 1) ? $this->output = 1 : $this->output = 0; ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/72818-solved-ternary-operator/#findComment-367262 Share on other sites More sharing options...
Aureole Posted October 11, 2007 Author Share Posted October 11, 2007 Thank you very much, I learn something new everyday here... Quote Link to comment https://forums.phpfreaks.com/topic/72818-solved-ternary-operator/#findComment-367263 Share on other sites More sharing options...
wildteen88 Posted October 11, 2007 Share Posted October 11, 2007 Don't forget ternary operators return values. So you could assign a variable a ternary operator, eg instead of doing this ($_SESSION['logged_in'] == 1) ? $this->output = 1 : $this->output = 0; You can do just this: $this->output = ($_SESSION['logged_in'] == 1) ? 1 : 0; Quote Link to comment https://forums.phpfreaks.com/topic/72818-solved-ternary-operator/#findComment-367334 Share on other sites More sharing options...
Aureole Posted October 12, 2007 Author Share Posted October 12, 2007 Oh I didn't know that, thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/72818-solved-ternary-operator/#findComment-367688 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.