mhpreiman
1/27/2018 - 3:38 PM

Shorthands & alternative syntax

PHP

If-else

(condition) ? valuetrue : valuefalse;
($isik1['age'] < $isik2['age']) ? -1 : 1;    //returns -1 if true, 1 if false
        

Switch or While:

switch ($i){      //or while

}
switch ($i):      //or while

endswitch;        //or endwhile

Accessing array:

$myArr[0]
$myArr{0}


Javascript

Accessing element by ID:

elementID

Example:

<span id='unicorn'>rainbow</span>   //changes rainbow to sparkly
unicorn.innerHTML = 'sparkly';

Accessing arrays and objects:

myArr[0]
myObj["propertyName"]
myObj.propertyName
myObj.methodName()

If-else + function boolean return

function myFunction() {
  return hours < 24 && minutes < 60 && seconds < 60;
}

Longer version:

function myFunction() {
  if (hours < 24 && minutes < 60 && seconds < 60)
      return true;
  else 
      return false;
}

If-else + variable boolean

var myVariable = hours < 24 && minutes < 60 && seconds < 60;

Longer version:

var myVariable;
if (hours < 24 && minutes < 60 && seconds < 60) 
    myVariable = true;
else 
    myVariable = false;

Ternary operator (variable example)

if (!maxOptions) {
  maxOptions = (type == 'number') ? chosenOptions : (!getOptionRange[2] ? minOptions : countOptions);    //all braces can be removed (at the cost readability)  
}

Longer version:

if (!maxOptions) {
  if (type == 'number') {   
    maxOptions = chosenOptions;
  }
  else if (!getOptionRange[2]) {
    maxOptions = minOptions;
  }
  else {
    maxOptions = countOptions;
  }
}