diff --git a/concepts/ternary-operator/.meta/config.json b/concepts/ternary-operator/.meta/config.json new file mode 100644 index 00000000..7426535a --- /dev/null +++ b/concepts/ternary-operator/.meta/config.json @@ -0,0 +1,5 @@ +{ + "blurb": "`ternary operator` concept in PHP", + "authors": ["neenjaw"], + "contributors": [] +} diff --git a/concepts/ternary-operator/about.md b/concepts/ternary-operator/about.md new file mode 100644 index 00000000..c6c1873a --- /dev/null +++ b/concepts/ternary-operator/about.md @@ -0,0 +1,67 @@ +# Ternary Operator + +The ternary operator provides an alternate syntax to perform an `if..else..` comparison and return a value. + +## Syntax + +PHP provides an if statement where an action can be evaluated on the basis of the "truthiness" of a condition. + +```php +if ($condition) { + $value = 1; +} else { + $value = 2 +} +``` + +Alternatively, the ternary operator can represent the same operation: + +```php +$value = $condition + ? 1 // True branch + : 2; // False branch +``` + +The comparison syntax is short, represented by `(condition) ? (evaluate if true) : (evaluate if false);`. + +## Benefits + +The obvious benefit of using a ternary operator is brevity, such that it is short to write the conditional expression compared to an `if..else..` statement, but there are several others as well: + +- It is an expression which can be used inside of another expression +- It facilitates lazy evaluation of each branch. + + ```php + function expensiveCalculation(): int + { + // An expensive calculation is performed + $answerToEverything = 42; + return $answerToEverything; + } + + $needExactAnswer = false; + $estimatedAnswer = 40 + $answer = $needExactAnswer ? expensiveCalculation() : $estimatedAnswer; + ``` + + In this example, if the exact answer is not required, the expensive calculation is not invoked. + +## Drawbacks + +Ternary operators have several drawbacks, making it important to consider their use: + +- A ternary operator can be more difficult to read as it depends on interpreting symbols rather than the use of keywords. +- A ternary operator may inadvertently lead to bugs because the ternary operator in PHP is left-associative in contrast to most contemporary languages (e.g. C, Java, JavaScript). +- The evaluated expression in the true or false branches must return a value. + +## Even Shorter + +The Ternary syntax can be shortened further to `(condition) ?: (evaluate if falsey)`. +One such use would be to be able to provide a fallback value in an expression: + +```php +$fruit = null; +eat($fruit ?: 'apple'); +``` + +This is also sometimes known as the _Elvis operator_ (`?:`) diff --git a/concepts/ternary-operator/introduction.md b/concepts/ternary-operator/introduction.md new file mode 100644 index 00000000..35cb9f60 --- /dev/null +++ b/concepts/ternary-operator/introduction.md @@ -0,0 +1,25 @@ +# Ternary Operator + +The ternary operator provides an alternate syntax to perform an `if..else..` comparison and return a value. + +## Syntax + +PHP provides an if statement where an action can be evaluated on the basis of the "truthiness" of a condition. + +```php +if ($condition) { + $value = 1; +} else { + $value = 2 +} +``` + +Alternatively, the ternary operator can represent the same operation: + +```php +$value = $condition + ? 1 // True branch + : 2; // False branch +``` + +The comparison syntax is short, represented by `(condition) ? (evaluate if true) : (evaluate if false);`. diff --git a/concepts/ternary-operator/links.json b/concepts/ternary-operator/links.json new file mode 100644 index 00000000..ced68121 --- /dev/null +++ b/concepts/ternary-operator/links.json @@ -0,0 +1,10 @@ +[ + { + "url": "https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary", + "description": "Comparison Operators: Ternary Operator" + }, + { + "url": "https://stitcher.io/blog/shorthand-comparisons-in-php", + "description": "Shorthand Comparisons in PHP" + } +] diff --git a/config.json b/config.json index c1282ac0..919951c2 100644 --- a/config.json +++ b/config.json @@ -856,6 +856,14 @@ "concepts": ["switch statement"], "prerequisites": [], "status": "beta" + }, + { + "name": "Ternary Operator", + "slug": "ternary-operator", + "uuid": "7c88c634-4ca9-4e4b-ab3f-6155b4ca9800", + "concepts": ["ternary operator"], + "prerequisites": [], + "status": "beta" } ], "key_features": [