Welcome to the Cookbook

loading...
Please login to continue

Ternary Operator

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

Ternary operators are permissible when the entire ternary operation fits on one line. Longer ternaries should be split into if else statements. Ternary operators should not ever be nested. Optionally parentheses can be used around the condition check of the ternary for clarity.

//Good, simple and readable
$variable = isset($options['variable']) ? $options['variable'] : true;

//Nested ternaries are bad
$variable = isset($options['variable']) ? isset($options['othervar']) ? true : false : false;

  1. //Good, simple and readable
  2. $variable = isset($options['variable']) ? $options['variable'] : true;
  3. //Nested ternaries are bad
  4. $variable = isset($options['variable']) ? isset($options['othervar']) ? true : false : false;