ternary-operator

What does ? … : … do? [duplicate]

放肆的年华 提交于 2019-12-27 12:01:51
问题 This question already has answers here : What are the PHP operators “?” and “:” called and what do they do? (9 answers) Closed 2 years ago . $items = (isset($_POST['items'])) ? $_POST['items'] : array(); I don't understand the last snippet of this code " ? $_POST['items'] : array(); " What does that combination of code do exactly? I use it to take in a bunch of values from html text boxes and store it into a session array. But the problem is, if I attempt to resubmit the data in text boxes

Ruby ternary operator

耗尽温柔 提交于 2019-12-25 11:24:30
问题 Why aren't these two statements equivalent? defined? foo ? foo << "bar" : foo = ["bar"] if (defined? foo) then foo << "bar" else foo = ["bar"] end First statement: irb(main):001:0> defined? foo ? foo << "bar" : foo = ["bar"] => nil irb(main):002:0> foo => nil irb(main):003:0> defined? foo ? foo << "bar" : foo = ["bar"] => "expression" irb(main):004:0> foo => ["bar"] Second statement: irb(main):001:0> if (defined? foo) then foo << "bar" else foo = ["bar"] end => ["bar"] irb(main):002:0> foo =>

Using ternary operator in angularjs to set an attribute on an element

旧时模样 提交于 2019-12-25 09:29:12
问题 I'm using angularjs 1.6 and required to use a specific css library that will disable a button if the button is defined like: <button disabled=""> My button </button> And will enable the button if defined: <button disabled=""> My button </button> I'm trying to enable/disable it based on the latest value from a property in a service within my controller, but the button is always showing as enabled even if someProperty is defined or not .. any ideas? <button {{ ctrl.myService.getData()

Using return as one of multiple statements in ternary expression [duplicate]

强颜欢笑 提交于 2019-12-25 07:58:33
问题 This question already has answers here : Why can't we have return in the ternary operator? (4 answers) Closed 8 months ago . I have this code: err ? (reject(err), return) : resolve(db) Which returns: SyntaxError: Unexpected token return However this works: err ? (reject(err), console.log('test')) : resolve(db) Why is that return can't be used in this situation? Is there other alternative to stop function execution while using ternary operator for multiple statements? 回答1: It's a ternary

C# ?? operator with DBNull (Coalesce-like result) [duplicate]

不想你离开。 提交于 2019-12-25 04:19:42
问题 This question already has answers here : Possible to use ?? (the coalesce operator) with DBNull? (6 answers) Closed 5 years ago . I'm getting DBNull results from DB. Trying to apply ?? operator to it like result["field1"] ?? result["field2"] field1 = DBNull field2 = 4 but it doesn't work, returns {} because result["field1"] is not null (it's DBNull). I expect to get that 4 from it. Tried to do result["field1"] = null first, but it doesn't work, it's still DBNull type. The question is how to

gcc inconsistent about string type [duplicate]

£可爱£侵袭症+ 提交于 2019-12-25 03:52:28
问题 This question already has answers here : Conditional operator used in cout statement (2 answers) Closed 5 years ago . I have the following test program: #include <string> #include <iostream> int main() { std::string s; std::string a = "sd"; std::cout << a==s ? "y" : "n"; return 0; } Trying to compile this with g++ test.cpp gives the following cryptic error: error: no match for 'operator==' (operand types are 'std::basic_ostream<char>' and 'std::string {aka std::basic_string<char>}') std::cout

Why does the Ruby ternary operator not allow extending and similar? [duplicate]

安稳与你 提交于 2019-12-25 02:43:16
问题 This question already has answers here : Return empty array (Ruby) (3 answers) Closed 5 years ago . I have two bits of code that, as far as I understand Ruby, should function identically. Both would sit within the same initialize method: class TicTacToePlayer def initialize(player_type = { human: true }) # Here end end The first code is a standard if/else statement: if player_type[:human] extend Human else extend Joshua end The second is just the above as a ternary operator: player_type[

What does this javascript code do? [duplicate]

↘锁芯ラ 提交于 2019-12-25 00:38:11
问题 This question already has answers here : Question mark and colon in JavaScript (7 answers) Closed 5 years ago . y = x?0:0x80 From googling the colon seems to be a ternary operator. 回答1: That's right. (The correct name is the conditional operator. It is a ternary operator, in that it takes three operands, but it's commonly misnamed the ternary operator because it's the only JavaScript operator that does so.) The code is roughly equivalent to this: var y; if (x) { y = 0; } else { y = 0x80; }

understading the ternary operator in php

一世执手 提交于 2019-12-24 17:19:03
问题 I'm reading someone else's code and they have a line like this: $_REQUEST[LINKEDIN::_GET_TYPE] = (isset($_REQUEST[LINKEDIN::_GET_TYPE])) ? $_REQUEST[LINKEDIN::_GET_TYPE] : ''; I just want to make sure I follow this. I might have finally figured out the logic of it. Is this correct? If $_REQUEST[LINKEDIN::_GET_TYPE] is set, then assign it to itself. (meant as a do-nothing condition) otherwise set it to a null string. (Would imply that NULL (undefined) and "" would not be treated the same in

Is it possible to put only one option on a ternary expression?

吃可爱长大的小学妹 提交于 2019-12-24 15:26:12
问题 I am just curious if this is possible or is there a way for this to become a valid syntax for C#: expression == value ? /*do nothing here, or put some empty block like { ; } */ : SomeClass.SomeMethod(); Edit : For an in-depth discussion and more information, I thought this block would work (if the dictionary key tested does not exist, it adds the dictionary. else, it would skip): (!packageDict.ContainsKey(desc)) ? packageDict.Add(desc, subtotal) : /*does nothing*/; 回答1: By looking at your