ternary-operator

How to make quick judgement and assignment without isset()?

六月ゝ 毕业季﹏ 提交于 2019-12-01 13:18:25
问题 I am tired of using code like: $blog = isset($_GET['blog']) ? $_GET['blog'] : 'default'; but I can't use: $blog = $_GET['blog'] || 'default'; Is there any way to do this without using isset() ? 回答1: You have to wait for the next version of PHP to get the coalesce operator // Uses $_GET['user'] if it exists -- or 'nobody' if it doesn't $username = $_GET['user'] ?? 'nobody'; // Loads some data from the model and falls back on a default value $model = Model::get($id) ?? $default_model; 回答2:

How wide-spread is this GNU extension on ternary operation?

对着背影说爱祢 提交于 2019-12-01 10:47:20
This is a GNU extension on ternary operation according to the Wikipedia . iMyVal = --iVal ?: iDft; While I'm fully aware that this is a GNU extension, but sometimes things may come in very handy with this special syntax. So, does anybody know if this syntax is only available in gcc ? Or are they any other compilers which support it? to anyone who's interested, PHP started supporting this syntax from 5.3 Thanks in advance. Some answers: GCC - yes MSVC - no (based on vanetto's answer) CLANG - no yes - the LLVM online compiler compiles it successfully. Intel C compiler - yes Bottom line - not

How wide-spread is this GNU extension on ternary operation?

丶灬走出姿态 提交于 2019-12-01 09:56:03
问题 This is a GNU extension on ternary operation according to the Wikipedia. iMyVal = --iVal ?: iDft; While I'm fully aware that this is a GNU extension, but sometimes things may come in very handy with this special syntax. So, does anybody know if this syntax is only available in gcc ? Or are they any other compilers which support it? to anyone who's interested, PHP started supporting this syntax from 5.3 Thanks in advance. 回答1: Some answers: GCC - yes MSVC - no (based on vanetto's answer) CLANG

Assigning variables by reference and ternary operator?

纵饮孤独 提交于 2019-12-01 05:24:17
Why ternary operator doesn't work with assignment by reference? $obj = new stdClass(); // Object to add $result = true; // Op result $success = array(); // Destination array for success $errors = array(); // Destination array for errors // Working $target = &$success; if(!$result) $target = &errors; array_push($target, $obj); // Not working $target = $result ? &$success : &$errors; array_push($target, $obj); John Here you go $target = ($result ? &$success : &$errors); Also your example has two typos edit http://php.net/manual/en/language.operators.comparison.php Note: Please note that the

Difference on address of const reference to ternary operator between clang and gcc

情到浓时终转凉″ 提交于 2019-12-01 03:51:30
问题 I have a vague idea of what's going on here... and it has to do with this but I'm wondering why clang++ and g++ handle this differently. Where is the undefined behaviour arround here? Note: this has nothing to do with templates - I just use them to make the example more compact. It's all about the type of whatever . #include <iostream> #include <vector> template <typename T> void test() { T whatever = 'c'; const char a = 'a'; std::cout << "begin: " << (void*)&a << std::endl; const char & me =

Conditionally assigning PHP values

泪湿孤枕 提交于 2019-12-01 02:46:30
For the very common case of assigning a value to a variable based on the outcome of an expression I'm a fan of ternary operators: $foo = $bar ? $a : b; However, if $bar is a relatively expensive operation and I want to assign the result of $bar to $foo if the result is truthy, this is inefficient: $foo = SomeClass::bigQuery() ? SomeClass::bigQuery() : new EmptySet(); One option is: $foo = ($result = SomeClass::bigQuery()) ? $result : new EmptySet(); But I'd rather not have the extra $result sitting in memory. The best option I've got is: $foo = ($foo = SomeClass::bigQuery()) ? $foo : new

Bug?? If you assign a value to a nullable integer via a ternary operator, it can't become null

こ雲淡風輕ζ 提交于 2019-12-01 00:26:22
dim val1 As Integer? = If(5 > 2, Nothing, 43) ' val1 = 0 dim val1 As Integer? = If(5 > 2, Nothing, Nothing) ' val1 = Nothing What gives? Is this a bug, or am I overlooking something? The problem is that Nothing in VB.NET works differently than, for example, null in C#. When Nothing is used in the context of a value type (such as Integer ) it represents the default value of that type. In this case, that's 0. In your first example, both branches of the ternary operator are valid Integer values. The true branch represents 0 and the false branch represents 43. In the second example, neither branch

Ternary operator without the middle expression

扶醉桌前 提交于 2019-11-30 23:54:56
问题 I realized recently that you can use the ternary operator in GCC and clang without a middle ( ?: or ? : works) and it will insert the first expression into the middle: // outputs 2 cout << (2 ?: 4); // outputs 3 cout << (0 ? : 3); Where is this in the standard? I looked and didn't see anything about it. 回答1: It isn't in the standard at all . What you are observing is a GCC extension: https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html If you omit it, its value is taken from the first operand

java ternary conditions strange null pointer exception [duplicate]

孤街醉人 提交于 2019-11-30 22:42:35
This question already has an answer here: Why does a ternary conditional expression returning null and assigned to a reference type cause a NullPointerException? [duplicate] 2 answers Can someone explain me why in the first case null pointer was detected, but no on the other ? Maybe he always looks on the first type, but why he does so only if the condition is false.. @Test public void test1() { final Integer a = null; final Integer b = false ? 0 : a; //===> NULL POINTER EXCEPTION } @Test public void test2() { final Integer b = false ? 0 : null; //===>NOT NULL POINTER EXCEPTION } @Test public

“Wrong” return type when using if vs. ternary opertator in Java

懵懂的女人 提交于 2019-11-30 22:01:23
问题 In the following class, the return type of the two methods is inconsistent with the idea that the ternary operator: return condition?a:b; is equivalent to if(condition) { return a; } else{ return b; } The first returns a Double and the second a Long: public class IfTest { public static Long longValue = 1l; public static Double doubleValue = null; public static void main(String[] args) { System.out.println(getWithIf().getClass());// outpus Long System.out.println(getWithQuestionMark().getClass