conditional-operator

?: operator (the 'Elvis operator') in PHP

蓝咒 提交于 2019-11-26 04:32:22
问题 I saw this today in some PHP code: $items = $items ?: $this->_handle->result(\'next\', $this->_result, $this); I\'m not familiar with the ?: operator being used here. It looks like a ternary operator, but the expression to evaluate to if the predicate is true has been omitted. What does it mean? 回答1: It evaluates to the left operand if the left operand is truthy, and the right operand otherwise. In pseudocode, foo = bar ?: baz; roughly resolves to foo = bar ? bar : baz; or if (bar) { foo =

Omitting the second expression when using the if-else shorthand

这一生的挚爱 提交于 2019-11-26 04:04:43
问题 Can I write the if else shorthand without the else ? var x=1; x==2 ? dosomething() : doNothingButContinueCode(); I\'ve noticed putting null for the else works (but I have no idea why or if that\'s a good idea). Edit: Some of you seem bemused why I\'d bother trying this. Rest assured it\'s purely out of curiosity. I like messing around with JavaScript. 回答1: This is also an option: x==2 && dosomething(); dosomething() will only be called if x==2 is evaluated to true. This is called Short

Compare multiple values in PHP

大憨熊 提交于 2019-11-26 03:44:19
问题 I\'d like to go from this: if($var == 3 || $var == 4 || $var == 5 || $var ==\'string\' || $var == \'2010-05-16\') { // execute code here } to this: if($var == (3, 4, 5, \'string\', \'2010-05-16\')) { // execute code here } Seems very redundant to keep typing $var , and I find that it makes it a bit cumbersome to read. Is there a way in PHP to do simplify it in this way? I read on a post here that when using XQuery you can use the = operator as in $var = (1,2,3,4,5) etc. 回答1: Place the values

The ternary (conditional) operator in C

久未见 提交于 2019-11-26 03:38:54
问题 What is the need for the conditional operator? Functionally it is redundant, since it implements an if-else construct. If the conditional operator is more efficient than the equivalent if-else assignment, why can\'t if-else be interpreted more efficiently by the compiler? 回答1: The ternary operator is a syntactic and readability convenience, not a performance shortcut. People are split on the merits of it for conditionals of varying complexity, but for short conditions, it can be useful to

Conditional operator differences between C and C++

◇◆丶佛笑我妖孽 提交于 2019-11-26 03:38:26
问题 I read somewhere that the ?: operator in C is slightly different in C++, that there\'s some source code that works differently in both languages. Unfortunately, I can\'t find the text anywhere. Does anyone know what this difference is? 回答1: The conditional operator in C++ can return an lvalue, whereas C does not allow for similar functionality. Hence, the following is legal in C++: (true ? a : b) = 1; To replicate this in C, you would have to resort to if/else, or deal with references

What is a Question Mark “?” and Colon “:” Operator Used for? [duplicate]

一个人想着一个人 提交于 2019-11-26 03:27:38
问题 This question already has an answer here: What is the Java ?: operator called and what does it do? 16 answers Two questions about using a question mark \"?\" and colon \":\" operator within the parentheses of a print function: What do they do? Also, does anyone know the standard term for them or where I can find more information on their use? I\'ve read that they are similar to an \'if\' \'else\' statement. int row = 10; int column; while (row >= 1) { column = 1; while(column <= 10) { System

?: ternary conditional operator behaviour when leaving one expression empty

旧城冷巷雨未停 提交于 2019-11-26 02:58:19
问题 I was writing a console application that would try to \"guess\" a number by trial and error, it worked fine and all but it left me wondering about a certain part that I wrote absentmindedly, The code is: #include <stdio.h> #include <stdlib.h> int main() { int x,i,a,cc; for(;;){ scanf(\"%d\",&x); a=50; i=100/a; for(cc=0;;cc++) { if(x<a) { printf(\"%d was too big\\n\",a); a=a-((100/(i<<=1))?:1); } else if (x>a) { printf(\"%d was too small\\n\",a); a=a+((100/(i<<=1))?:1); } else { printf(\"%d

Operator precedence with Javascript Ternary operator

六眼飞鱼酱① 提交于 2019-11-26 02:32:14
问题 I cant seem to wrap my head around the first part of this code ( += ) in combination with the ternary operator. h.className += h.className ? \' error\' : \'error\' The way i think this code works is as following: h.className = h.className + h.className ? \' error\' : \'error\' But that isn\'t correct because that gives a error in my console. So my question is how should i interpet this code correctly? 回答1: h.className = h.className + (h.className ? ' error' : 'error') You want the operator to

How to check if my string is equal to null?

前提是你 提交于 2019-11-26 02:27:58
问题 I want to perform some action ONLY IF my string has a meaningful value. So, I tried this. if (!myString.equals(\"\")) { doSomething } and this if (!myString.equals(null)) { doSomething } and this if ( (!myString.equals(\"\")) && (!myString.equals(null))) { doSomething } and this if ( (!myString.equals(\"\")) && (myString!=null)) { doSomething } and this if ( myString.length()>0) { doSomething } And in all cases my program doSomething in spite on the fact that my string IS EMPTY. It equals to

Booleans, conditional operators and autoboxing

孤人 提交于 2019-11-26 02:19:00
问题 Why does this throw NullPointerException public static void main(String[] args) throws Exception { Boolean b = true ? returnsNull() : false; // NPE on this line. System.out.println(b); } public static Boolean returnsNull() { return null; } while this doesn\'t public static void main(String[] args) throws Exception { Boolean b = true ? null : false; System.out.println(b); // null } ? The solution is by the way to replace false by Boolean.FALSE to avoid null being unboxed to boolean --which isn