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 @@ + + + + + + + Ternary Operator + + + + +

Ternary Operator

+ +

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:

+ + + +

Example #1

+ +

At this point, recall the "if-else" statement, which consists of:

+ +
    +
  1. An "if" statement, which is executed when the condition is true; and
  2. +
  3. An "else" statement, which is executed when the condition is false.
  4. +
+ +

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:

+ + + +

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.

+ +

Example #2

+ +

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.

+ +

Example #3

+ +

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!

+ +

Conclusion

+ +

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.

+ +

References

+ + + + diff --git a/samples/ternary-operator/style.css b/samples/ternary-operator/style.css new file mode 100644 index 0000000..997abe3 --- /dev/null +++ b/samples/ternary-operator/style.css @@ -0,0 +1,66 @@ +body { + font-size: 100%; + margin: auto; + width: 800px; +} + +h1, h2, p, ul, ol { + font-family: "Segoe UI Light", Calibri, Roboto, Helvetica, sans-serif; +} + +h1, h2 { + color: #2874A6; + padding-bottom: 6px; + border-bottom-style: solid; + border-bottom-width: 1px; + border-bottom-color: #2874A6; +} + +p, ul, ol, code { + font-size: 1rem; +} + +h1 { + font-size: 2rem; + font-weight: bold; + text-transform: uppercase; + text-align: center; +} + +h2 { + font-size: 1.25rem; + font-weight: bold; +} + +a { + color: #2874A6; + font-weight: bold; + text-decoration: none; +} + +a:hover { + color: #2874A6; + text-decoration: underline; +} + +.blue { + color: #2874A6; +} + +.green { + color: #1E8449; +} + +.red { + color: #B03A2E; +} + +.snippet { + border-radius: 10px; + background-color: #EBF5FB; +} + +.result { + border-radius: 10px; + background-color: #FEF9E7; +}