Ternary Operator

Most times the same action can be done with the ternary operator. The following lines of code show same conditional usages.

if ($i === 2) {
    $j = 0;
} else {
    ++$j;
}

With ternary operator you can do a one-liner:

$j = ($i === 2) ? 0 : ++$j;


Found a typo? Something wrong with this content?

Just fork and edit it.

Content of this work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. Code snippets in examples are published under the CC0 1.0 Universal (CC0 1.0). Thanks to all contributors.