ternary-operator

How do I use the ternary operator ( ? : ) in PHP as a shorthand for “if / else”?

送分小仙女□ 提交于 2019-11-26 00:17:55
问题 Based on the examples from this page, I have the working and non-working code samples below. Working code using if statement: if (!empty($address[\'street2\'])) echo $address[\'street2\'].\'<br />\'; Non-working code using ternary operator: $test = (empty($address[\'street2\'])) ? \'Yes <br />\' : \'No <br />\'; // Also tested this (empty($address[\'street2\'])) ? \'Yes <br />\' : \'No <br />\'; UPDATE After Brian\'s tip, I found that echoing $test outputs the expected result. The following

How do you use the ? : (conditional) operator in JavaScript?

本秂侑毒 提交于 2019-11-25 23:56:43
问题 Can someone please explain to me in simple words what is the ?: (conditional, \"ternary\") operator and how to use it? 回答1: This is a one-line shorthand for an if-else statement. It's called the conditional operator. 1 Here is an example of code that could be shortened with the conditional operator: var userType; if (userIsYoungerThan18) { userType = "Minor"; } else { userType = "Adult"; } if (userIsYoungerThan21) { serveDrink("Grape Juice"); } else { serveDrink("Wine"); } This can be

To ternary or not to ternary? [closed]

☆樱花仙子☆ 提交于 2019-11-25 23:53:48
问题 I\'m personally an advocate of the ternary operator: () ? : ; I do realize that it has its place, but I have come across many programmers that are completely against ever using it, and some that use it too often. What are your feelings on it? What interesting code have you seen using it? 回答1: Use it for simple expressions only : int a = (b > 10) ? c : d; Don't chain or nest ternary operators as it hard to read and confusing: int a = b > 10 ? c < 20 ? 50 : 80 : e == 2 ? 4 : 8; Moreover, when

Does Python have a ternary conditional operator?

狂风中的少年 提交于 2019-11-25 22:51:44
问题 If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs? 回答1: Yes, it was added in version 2.5. The expression syntax is: a if condition else b First condition is evaluated, then exactly one of either a or b is evaluated and returned based on the Boolean value of condition . If condition evaluates to True , then a is evaluated and returned but b is ignored, or else when b is evaluated and returned but a is ignored. This allows

What is the Java ?: operator called and what does it do?

余生颓废 提交于 2019-11-25 22:16:36
问题 I have been working with Java a couple of years, but up until recently I haven\'t run across this construct: int count = isHere ? getHereCount(index) : getAwayCount(index); This is probably a very simple question, but can someone explain it? How do I read it? I am pretty sure I know how it works. if isHere is true, getHereCount() is called, if isHere is false getAwayCount() is called. Correct? What is this construct called? 回答1: Yes, it is a shorthand form of int count; if (isHere) count =

How to write a PHP ternary operator

折月煮酒 提交于 2019-11-25 19:36:03
How do I write a PHP ternary operator with the elseif portion? I see basic examples with the if and else portions of the PHP ternary operator like this: echo (true) ? "yes" : "no"; //prints yes echo (false) ? "yes" : "no"; //prints no How do I get the "elseif" portion like this into the ternary operator? <?php if($result->vocation == 1){ echo "Sorcerer"; }else if($result->vocation == 2){ echo 'Druid'; }else if($result->vocation == 3){ echo 'Paladin'; }else if($result->vocation == 4){ echo 'Knight'; }else if($result->vocation == 5){ echo 'Master Sorcerer'; }else if($result->vocation == 6){ echo

PHP ternary operator vs null coalescing operator

爷,独闯天下 提交于 2019-11-25 18:44:40
Can someone explain the differences between ternary operator shorthand ( ?: ) and null coalescing operator ( ?? ) in PHP? When do they behave differently and when in the same way (if that even happens)? $a ?: $b VS. $a ?? $b MasterOdin When your first argument is null, they're basically the same except that the null coalescing won't output an E_NOTICE when you have an undefined variable. The PHP 7.0 migration docs has this to say: The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first