diff --git a/samples/ternary-operator/index.php b/samples/ternary-operator/index.php new file mode 100644 index 0000000..0838864 --- /dev/null +++ b/samples/ternary-operator/index.php @@ -0,0 +1,327 @@ + + + + +
+ +The ternary operator is a special type of conditional operator. A statement using the ternary operator takes the form of:
+ +[Expression #1] ? [Expression #2] : [Expression #3];
+
+ Where:
+ +Expression #1 sets a condition that is either true or false;Expression #2 is executed if the condition is true; andExpression #3 is executed if the condition is false.At this point, recall the "if-else" statement, which consists of:
+ +Lecture #9 contains an example of an "if-else" statement that determines whether someone is old enough to drink beer:
+ +
+
+ $age = 20;
+
+ if ($age >= 21) {
+ print "You can drink legally! :)";
+ }
+
+ else {
+ print "Sorry, no beer for you. :(";
+ }
+
When we run the above code, we get:
+ +
+
+ = 21) {
+ print "You can drink legally! :)";
+ }
+
+ else {
+ print "Sorry, no beer for you. :(";
+ }
+ ?>
+
+ A ternary operator is essentially a shortcut for writing an "if-else" statement.
+ +Rewriting the drinking age "if-else" statement example using the ternary operator:
+ +Expression #1 would be whether the person's age is equal to or greater than 21 years old;Expression #2 would display "You can drink legally! :)" if Expression #1 is true; andExpression #3 would display "Sorry, no beer for you. :(" if Expression #1 is false.Therefore, an example of the ternary operator that determins whether someone is old enough to drink beer would look like this:
+ +
+
+ $age = 20;
+
+ $age >= 21 ? print "You can drink legally! :)" : print "Sorry, no beer for you. :(";
+
When we run the above code, we get the same result:
+ +
+
+ = 21 ? print "You can drink legally! :)" : print "Sorry, no beer for you. :(";
+ ?>
+
+ As you can see (including white space), 9 lines of code written using an "if-else" statement is condensed into 3 lines of code written using an ternary operator.
+ +Here's another example of an "if-else" statement, which takes a number and returns the absolute value of that number:
+ +
+
+ $number = -12;
+
+ if ($number >= 0) {
+ $absolute_value = $number;
+ }
+
+ else {
+ $absolute_value = -$number;
+ }
+
+ echo "The absolute value of $number is $absolute_value.";
+
When we run the above code, we get:
+ +
+
+ = 0) {
+ $absolute_value = $number;
+ }
+
+ else {
+ $absolute_value = -$number;
+ }
+
+ echo "The absolute value of $number is $absolute_value.";
+ ?>
+
+ And here's how we would use the ternary operator to condense the above "if-else" statements:
+ +
+
+ $number = -12;
+
+ $absolute_value = ($number >= 0) ? $number : -$number;
+
+ echo "The absolute value of $number is $absolute_value.";
+
When we run the above code, we get the same result: + +
+ + = 0) ? $value : -$value; + + echo "The absolute value of $number is $absolute_value."; + ?> + +
In this example, we used the ternary operator to figure out the absolute value of a given number and then assign the result to the variable $absolute_value. And rather than using 7 lines of code (counting white space), this was accomplished using only 1 line of code.
It's possible to "nest" (or, for readability, "stack") multiple ternary operators within each other. For example, let's take the following "if-else" statements:
+ +
+
+ $arg = "B";
+
+ if ($arg == "R") {
+ $exercise = "running";
+ }
+
+ elseif ($arg == "B") {
+ $exercise = "biking";
+ }
+
+ elseif ($arg == "S") {
+ $exercise = "swimming";
+ }
+
+ else {
+ $exercise = "walking";
+ }
+
+ echo $exercise;
+ Note that we can also write the above series of "if-else" statements using a "switch" statement:
+ +
+
+ $arg = "B";
+
+ switch ($arg) {
+ case "R":
+ $exercise = "running";
+ break;
+ case "B":
+ $exercise = "biking";
+ break;
+ case "S":
+ $exercise = "swimming";
+ break;
+ default:
+ $exercise = "walking";
+ }
+
+ echo $exercise;
+ In both cases, when we run the code, we get:
+ ++ + + +
When writing the above "if-else" / "switch" statements using ternary operators, we might be tempted to "stack" multiple ternary operators in the following fashion:
+ +
+
+ $arg = "B";
+
+ $exercise = ( ($arg == "R") ? "running" :
+ ($arg == "B") ? "biking" :
+ ($arg == "S") ? "swimming" :
+ "walking" );
+
+ echo $exercise;
+
However, running this code would return "swimming" rather than "biking":
+ ++ + + +
That's because the conditional operator in PHP is left associative. To avoid this, simply use the following template when "stacking" ternary operators:
+ +
+
+ $arg = "B";
+
+ $exercise = $arg == "R" ? "running" :
+ ($arg == "B" ? "biking" :
+ ($arg == "S" ? "swimming" :
+ "walking" ));
+
+ echo $exercise;
+
Running this code would return "biking", as expected:
+ ++ + + +
Whereas the "if-else" statements took 19 lines of code and the "stack" statement took 17 lines of code, using the ternary operator allowed us to achieve the same thing using only 8 lines of code!
+ +The benefits of using the ternary operator is being able to write "if-else" logic more quickly and in fewer lines of code. This is particularly useful when dealing with simple "if-else" statements or "switch" statements.
+ +However, it might be impractical and hazardous to use the ternary operator for more complicated "if-else" logic, where "if-else" statements are nested within each other. (In such instances, it would also be impractical and hazardous to use a "switch" statement.)
+ +Another concern of using the ternary operator is readability, especially when using the ternary operator as shorthand for a "switch" statement. When nesting the ternary operator, "stack" them rather than squeezing everything in one line and pay attention to parenthesis use.
+ +Finally, when working in a group, make sure other members of your team are aware of and understand the ternary operator before you incorporate it into your code.
+ +