ternary-operator

Can you pass by reference while using the ternary operator?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 11:54:11
Simple question, simple code. This works: $x = &$_SESSION['foo']; This does not: $x = (isset($_SESSION['foo']))?&$_SESSION['foo']:false; It throws PHP Parse error: syntax error, unexpected '&' . Is it just not possible to pass by reference while using the conditional operator, and why not? Also happens if there's a space between the ? and & . Simple answer: no. You'll have to take the long way around with if/else. It would also be rare and possibly confusing to have a reference one time, and a value the next. I would find this more intuitive, but then again I don't know your code of course: if

Ternary Operators. Possible for a one sided action?

旧街凉风 提交于 2019-11-28 10:58:29
I've used ternary operators for a while and was wondering if there was a method to let say call a function without the else clause. Example: if (isset($foo)) { callFunction(); } else { } Now obviously we can leave out the else to make: if (isset($foo)) { callFunction(); } Now for a ternary How can you 'by pass' the else clause if the condition returns false? isset($foo) ? callFunction() : 'do nothing!!'; Either a mystery or not possible? Short-circuit isset($foo) and callFunction(); Reverse the condition and omit the second argument !isset($foo) ?: callFunction(); or return just "something"

How to use ternary operator instead of if-else in PHP

拈花ヽ惹草 提交于 2019-11-28 10:21:58
I’m trying to shorten my code using the ternary operator. This is my original code: if ($type = "recent") { $OrderType = "sid DESC"; } elseif ($type = "pop") { $OrderType = "counter DESC"; } else { $OrderType = "RAND()"; } How can I use the ternary operator in my code instead of if s/ else s? $OrderType = ($type = "recent") ? "sid DESC" : "counter DESC" ; This is the code I tried, but have no idea how to add an “ elseif part” to it. This is called the ternary operator ;-) You could use two of those : $OrderType = ($type == 'recent' ? 'sid DESC' : ($type == 'pop' ? 'counter DESC' : 'RAND()'))

VB.NET - Nullable DateTime and Ternary Operator

陌路散爱 提交于 2019-11-28 10:11:43
I'm having problems with a Nullable DateTime in VB.NET (VS 2010). Method 1 If String.IsNullOrEmpty(LastCalibrationDateTextBox.Text) Then gauge.LastCalibrationDate = Nothing Else gauge.LastCalibrationDate = DateTime.Parse(LastCalibrationDateTextBox.Text) End If Method 2 gauge.LastCalibrationDate = If(String.IsNullOrEmpty(LastCalibrationDateTextBox.Text), Nothing, DateTime.Parse(LastCalibrationDateTextBox.Text)) When given an empty string Method 1 assigns a Null (Nothing) value to gauge.LastCalibrationDate but Method 2 assigns it the DateTime.MinValue. In other places in my code I have:

Difference between using a ternary operator or just short-circuit evaluation?

≡放荡痞女 提交于 2019-11-28 09:52:16
问题 Recently came across short-circuit evaluation and was a little confused by it as i only got into programming the past week. From what i understand if what ever comes before the first double pipe is true then it will stop and not evaluate what comes after the double pipe. For Example: Example 1: var a = true; var b = a || {}; So i assume if a exists then assign a to b otherwise b is equal to an object. What i dont understand is where i will use this and how it differs to a ternary operator,

ORACLE IIF Statement

天大地大妈咪最大 提交于 2019-11-28 09:37:58
I get an error while writing the IIF stamtement, table and the statement given below, Statement: SELECT IIF(EMP_ID=1,'True','False') from Employee; Error: 00907-missing right parantheses CREATE TABLE SCOTT.EMPLOYEE ( EMP_ID INTEGER NOT NULL, EMP_FNAME VARCHAR2(30 BYTE) NOT NULL, EMP_LNAME VARCHAR2(30 BYTE) NOT NULL, EMP_ADDRESS VARCHAR2(50 BYTE) NOT NULL, EMP_PHONE CHAR(10 BYTE) NOT NULL, EMP_GENDER CHAR(1 BYTE) ) Please provide your inputs. Mt. Schneiders Oracle doesn't provide such IIF Function. Instead, try using one of the following alternatives: DECODE Function : SELECT DECODE(EMP_ID, 1,

Calling functions in C# ternary operator [duplicate]

让人想犯罪 __ 提交于 2019-11-28 09:22:21
问题 This question already has answers here : C# Conditional Operator Not a Statement? (9 answers) Closed 3 years ago . Why is this code not valid? Pretty sure it's legit in C /C++ Pseudocode: String s = Console.ReadLine(); int x = 0; Int32.TryParse(s, out x) ? Console.WriteLine("Foo") : Console.WriteLine("bar"); 回答1: The ternary operator is used to return values and those values must be assigned. If you want to invoke void methods in a ternary operator, you can use delegates like this: String s =

Why ternary operation gives nullpointer while its ifelse counterpart doesn't? [duplicate]

拥有回忆 提交于 2019-11-28 09:08:17
This question already has an answer here: Booleans, conditional operators and autoboxing 4 answers I am getting NullPointerException in one instance below while its counterpart runs smooth. public static void main(String[] args){ System.out.println(withTernary(null, null)); //Null Pointer System.out.println(withIfElse(null, null)); //No Exception } private static Boolean withTernary(String val, Boolean defVal){ return val == null ? defVal : "true".equalsIgnoreCase(val); } private static Boolean withIfElse(String val, Boolean defVal){ if (val == null) return defVal; else return "true"

Troubleshooting “Unexpected T_ECHO” in ternary operator statement

这一生的挚爱 提交于 2019-11-28 08:59:43
($DAO->get_num_rows() == 1) ? echo("is") : echo("are"); This dose not seem to be working for me as intended, I get an error "Unexpected T_ECHO". I am expecting it to echo either 'is' or 'are'. I have tried it without the brackets around the conditional. Am I just not able to use a ternary operator in this way? The $DAO->get_num_rows() returns an integer value. The Ternary operator is not identical to an if-then. You should have written it echo ($DAO->get_num_rows() == 1) ? "is" : "are"; It returns the value in the 2nd or 3rd position. It does NOT execute the statement in the 2nd or 3rd

The Ternary Operator in PHP [duplicate]

大城市里の小女人 提交于 2019-11-28 08:34:37
问题 This question already has answers here : How to concatenate multiple ternary operator in PHP? (8 answers) Closed 4 years ago . $chow = 3; echo ($chow == 1) ? "one" : ($chow == 2) ? "two" : "three"; output: three $chow = 1; echo ($chow == 1) ? "one" : ($chow == 2) ? "two" : "three"; output: two Can anyone explain why the output is "two" when $chow = 1 instead of "one"? 回答1: This is because the ternary operator ( ?: ) is left associative so this is how it's getting evaluated: ((1 == 1) ? "one"