Questions tagged [shorthand-if]

45 questions
12
votes
2 answers

shorthand php if{} ELSE IF{} else{}

is there a possibility to write a shorthand if, ELSE IF, else statement for php. if / else is clear but is there a shorthanded way when i want to use elseif too (except switch)? if ($typeArray == 'date'){ echo 'date'; } else if($typeArray ==…
Timotheus0106
  • 1,536
  • 3
  • 18
  • 28
8
votes
3 answers

JavaScript Design Patterns Help Needed: Loose Augmentation of Modules

Edit for clarity - @Qantas94Heavy - I understand what it is "saying" or supposed to do, what I don't understand is why & more importantly how it works: I was reading an advanced tutorial on the JS Module Pattern, and it gave this example: var MODULE…
6
votes
5 answers

c# If else shorthand

In c# can I do something like this in a shorthand? bool validName = true; if (validName) { name = "Daniel"; surname = "Smith"; } else { MessageBox.Show("Invalid name"); } I was just wondering if something similar to this would…
adminSoftDK
  • 2,012
  • 1
  • 21
  • 41
6
votes
7 answers

Is there JS shorthand for conditional "not equal to a and not equal to b" and such?

I was just wondering if there is some JS shorthand for something like this: if (x != 1 && x != 2) do stuff; Does such a beast exist? Instead, I want to say something like this: if (x != 1:2) do stuff;
5
votes
1 answer

String check with if js shorthand

I have var names = []; and want push there some string only if it's not empty. Is there way make it with some shorthand method in js?) That i have now. if ("" != opportunityName) { names.push(opportunityName); } And that Don't wotk…
Maks Martynov
  • 460
  • 3
  • 18
4
votes
2 answers

Ternary operator when casting a variable

While writing is_numeric($var) ? (Int)$var : (String)$var;, I was wondering if it could be possible to move the ternary operator to the part where I cast the variable: echo (is_numeric($var) ? Int : String)$var; Not to my surprise, it didn't…
Bas Peeters
  • 3,269
  • 4
  • 33
  • 49
4
votes
3 answers

Shorthand if then else performance/optimization in php

Talking about PHP, i would like to ask if there is a difference in performance between these two: $name=($IsBoy)?"George":"Mary"; vs if($IsBoy) { $name="George"; } else { $name="Mary"; } Will these two result to different opcode? If yes,…
Sharky
  • 6,154
  • 3
  • 39
  • 72
3
votes
4 answers

How to write in shorthand form if / else if / else?

Is there a shorthand for an if / else if / else statement? For example, I know there's one for an if / else statement: var n = $("#example div").length; $("body").css("background", (n < 2) ? "green" : "orange"); But how do I write the following in…
GTS Joe
  • 3,612
  • 12
  • 52
  • 94
3
votes
3 answers

Shorthand if ? return : null

I want to achieve this: if (full) { return } else{ // nuthin } But shorter, something like: full ? return : null; But that doesn't work.. I could do: if (full) { return } But I like the ternary more I expected something…
TrySpace
  • 2,233
  • 8
  • 35
  • 62
3
votes
4 answers

nested shorthand if

I have a little trouble with a shorthanded if statement I can't figure out ($product == "vindo") ? $this->getNextVindoInList($id) : $this->getNextGandrupInList($id), This works fine but I want to have another check in that statement. Like…
Morten Hagh
  • 2,055
  • 8
  • 34
  • 66
2
votes
2 answers

Multiple statements if condition is true in shorthand if

I recently discovered the shorthand if statement and after searching online I couldn't find a definite answer. Is it possible to execute 2 statements if the condition is true/false? int x = (expression) ? 1 : 2; for example int x = (expression) ?…
Patrick
  • 351
  • 1
  • 4
  • 15
2
votes
1 answer

Does c# have a shorthand "something if (condition);" statement?

Simple and short: does C# have a shorthand if statement similar to Ruby's? return if (condition)
Redoman
  • 3,059
  • 3
  • 34
  • 62
2
votes
2 answers

Not sure what the value of a is

where: $b = true; $c = 0; $a = ($a ? ($a ? $b : $c) : ($c ? $a : $b)); I'm not sure how to work out a. So I understand that this is a shorthand operator, and usually it's a case of: $value ? true : false meaning if $a = true { true } else { false…
2
votes
1 answer

php if/else shorthand fail

Normal expression works fine, shorthand doesn't. Where am I wrong here? if (isset($var)) $value = $var; elseif ($str !== 'string') $value = $str; else $value = null; // works just fine $value = (isset($var)) ? $var : ($str !== 'string') ? $str :…
nxet
  • 738
  • 2
  • 10
  • 22
2
votes
2 answers

Value variable as 0 not as false in shorthand-if expression

I'm calling a function in either those two ways foo([x,y]) or foo({x:x,y:y}) with x,y ∈ [0,∞) the foo function looks like this var x = pos.x || pos[0], y = pos.y || pos[1]; If I call the function the second way with x=0 then pos.x will be…
InsOp
  • 2,425
  • 3
  • 27
  • 42
1
2 3