Questions tagged [shorthand-if]

47 questions
0
votes
5 answers

Echo with Shorthand If not behaving properly

I'm programming in PHP, I'm using a 'shorthand if' to echo some HTML code on to the page, but it is behaving in odd ways. echo '
0
votes
2 answers

shorthand if/else variable exists

I'm simply running a function which checks if the variable year is set if not then set it new Date().getFullYear(). The error I get: Uncaught ReferenceError: year is not defined year = (year) ? year : new…
Jordan Davis
  • 1,197
  • 4
  • 16
  • 36
0
votes
1 answer

Shorthand if statement for property initialization in Javascript

To make my code cleaner I do the following in a javascript file: var _weight; function getWeight(){ _weight || InitializeWeight(); // do some stuff return _weight; } function InitializeWeight(){ _weight = 3; } JSHint throws an…
kawa
  • 65
  • 1
  • 2
  • 11
0
votes
1 answer

JS shorthand If doing more than one operations

a==b? do 'x' : do 'y' ; works fine how would you write to do two things for example? > a==b? do 'x' and do 'z' : do y ;
1234
  • 231
  • 1
  • 2
  • 13
0
votes
4 answers

initialization of two variables at the same time

Is there any way to initialize these two variable at the same time? in this example "time" variable can be "" or has a value. var variable1 = string.IsNullOrEmpty(time) ? string.Empty : "value"; var variable2 = string.IsNullOrEmpty(time) ? "value" :…
Mina
  • 377
  • 1
  • 4
  • 11
0
votes
2 answers

How does shorthand if/else work? [PHP]

I've been trying to translate my PHP to the shorthand version, just for the fun of learning something new, but I can't get mine to work. I've looked at the many other questions about this on SO, but I couldn't adapt any of them to my situation. What…
Rvervuurt
  • 7,171
  • 7
  • 35
  • 53
0
votes
5 answers

if (true) Shorthand

Is there a shorthand when checking a boolean for true? Example: if (autoConnect) Connect(); We can do return IsOpen() ? true : false; But I cant get autoConnect ? Connect(); running. Is there a way to do this?
Kimmax
  • 1,590
  • 19
  • 30
0
votes
1 answer

Why lvalue is required in shorthand if-else in the else part when using a assignment operator?

#include void main() { int k = 8; int m = 7; int z = k < m ? k = m : m++; printf("%d", z); k = 8; m = 7; z = k < m ? m++ : k=m; printf("%d", z); …
Pankaj Mahato
  • 983
  • 4
  • 12
  • 25
0
votes
1 answer

Javascript shorthand for true or false in function

Is there a shorthand way of doing this so that the outcome is always true or false? function trueFalse() { if( a == 1 ) { return true; }else{ return false; } } Something like return true:false; and no need for the else section? Thanks.
martin
  • 363
  • 1
  • 5
  • 19
0
votes
1 answer

IF/ELSE PHP shorthand

I'm struggling the past couple of hours to write in shorthand the PHP conditional statement below: public static $url = $_SERVER['HTTP_REFERER']; if (false !== strpos($url,'en')) { $currlang = 'en'; } else { $currlang = 'fr'; } I can't…
otinanai
  • 3,769
  • 2
  • 21
  • 42
0
votes
2 answers

jquery: shorter code (if & var)

that's probably a silly question, but i have a problem not with the code not functioning - it's rather the code is too long... everything works fine but for example is there a way to shorten the if/else ? or is it possible to track 4 different var…
dyb
  • 59
  • 7
0
votes
3 answers

Shorthand-if with character index check

I am using a shorthand if statement to check if content has an italic tag in it. remove = (content.indexOf('') === true) ? true : false; alert("ORIGINAL CONTENT: " + content + "\nDoes content contain ? " + remove); When that alert pops up, it…
Bird87 ZA
  • 2,259
  • 8
  • 33
  • 62
-1
votes
6 answers

How to read spagetti php shorthand if statement ( ternary operators) and conver it into if and else statement

Someone wrote this 'stunning' short hand encapsulated statement. I am sure its not even suppose to be done like this, but I want to dissect it for someone else (myself as well) to be able to understand it well by break it down into if and else…
Ezeewei
  • 6,347
  • 7
  • 50
  • 98
-1
votes
5 answers

Shorthand if statement PHP

This is a really basic question and I appoligize for asking, but it would be really usefull for me to understand, so I can use this method in the future, How would I turn if($_GET['page']) { $page = $_GET['page']; } else { $page = 0; } Into a…
Curtis Crewe
  • 3,564
  • 5
  • 24
  • 30
-2
votes
5 answers

shorthand if explanation ? mark operator

For a template engine I would like to use shorthand if condition. I need to check if a value is != null print out some line if true. What I tried: echo "test" ($user->affiliate_id !=null) ? I have no idea what to write behind the ?.
peke_peke
  • 391
  • 5
  • 18