From 7ff55681e33334f75c19b8cc8ef9fe4dbe0fca05 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 10 May 2017 08:14:38 -0400 Subject: [PATCH 1/3] explain ternary operator --- samples/ternary-operator/index.php | 327 +++++++++++++++++++++++++++++ samples/ternary-operator/style.css | 66 ++++++ 2 files changed, 393 insertions(+) create mode 100644 samples/ternary-operator/index.php create mode 100644 samples/ternary-operator/style.css 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; +} From 2c8eda7549f3926d9a337c02f6a951afc3130952 Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 11 May 2017 16:56:46 -0400 Subject: [PATCH 2/3] updated lecture 10 sample --- samples/lecture10/index.php | 82 +++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/samples/lecture10/index.php b/samples/lecture10/index.php index 400b30f..b0b6c22 100644 --- a/samples/lecture10/index.php +++ b/samples/lecture10/index.php @@ -238,6 +238,88 @@ function custom_greeting($greeting, $name) { ?> + +
+ <2>Default Argument Values +
+ +

Sometimes, it's helpful to assign a "default" argument value to your functions. If the argument value you're using is the same most of the time, having a default argument value written into your function will save you time.

+ +

To assign a default argument value to your function, simply initialize the argument with the default value, like this:

+ + + I had $fruit $food for breakfast.

"; + } + + // Calling the function without passing any argument values to the function will result in the output "I had orange juice for breakfast.": + breakfast(); + + // Calling the function again, this time passing "orange" and "juice" to the function will result in same output, except we had to type out "orange" and "juice": + breakfast("orange", "juice"); + + // Calling the function, this time passing "apple" and "pie" to the function, will override the default argument values and result in the output "I had apple pie for breakfast.": + breakfast("apple", "pie"); + + // Calling the function without passing any argument values to the function, again, will result in the output we had before with the default argument values: + breakfast(); + ?> +
+ +

To have a function with a default argument value return nothing, as if you you didn't pass any value into a function without a default argument value specified, you can use pass "null" into the function. For example:

+ + + Thank you, $name!"; + } + + // Calling the function without passing any argument values to the function will result in the output "Thank you, !": + thankyou1(); + + // Calling the function while passing "Bob" to the function will result in the output "Thank you, Bob!": + thankyou1("Bob"); + + // Define the function "thankyou2" WITH a default argument value set to "Bob": + function thankyou2($name = "Bob") { + echo "

Thank you, $name!"; + } + + // To obtain the output "Thank you, Bob!", we can simply call the function without passing any argument values to it: + thankyou2(); + + // To obtain the output "Thank you, !", we'll have to pass "null" into the function: + thankyou2(null); + ?> + + +

When there are multiple parameters to your function, you can have SOME default argument values while leaving others blank. In such cases, please keep in mind that PHP fills its parameters FROM LEFT TO RIGHT. For example:

+ + + I live in $region $neighborhood.

"; + } + + // Calling the function without passing any argument values to it will result in "I live in Harlem.": + myhood(); + + // Calling the function while passing just one argument, "West" to it will result in "I live in West Harlem." because the one argument fills the first parameter, $region: + myhood("West"); + + // If you want the output "I live in Greenwich Village", i.e. replace "Harlem" with "Greenwich Village" while not specifying a region, this will NOT work--the output will be "I live in Greenwich Village Harlem" because the first argument fills the first parameter: + myhood("Greenwich Village"); + + // The correct way would be to utilize the "null" value, like this: + myhood(null, "Greenwich Village"); + ?> +
+
+

Function Return Values

From ccc6f1a4a601044b89ff103660963601fd5adb2b Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 11 May 2017 17:03:26 -0400 Subject: [PATCH 3/3] without ternary operator --- samples/ternary-operator/index.php | 327 ----------------------------- samples/ternary-operator/style.css | 66 ------ 2 files changed, 393 deletions(-) delete mode 100644 samples/ternary-operator/index.php delete mode 100644 samples/ternary-operator/style.css diff --git a/samples/ternary-operator/index.php b/samples/ternary-operator/index.php deleted file mode 100644 index 0838864..0000000 --- a/samples/ternary-operator/index.php +++ /dev/null @@ -1,327 +0,0 @@ - - - - - - - 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:

- -
    -
  • Expression #1 sets a condition that is either true or false;
  • -
  • Expression #2 is executed if the condition is true; and
  • -
  • Expression #3 is executed if the condition is false.
  • -
- -

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:

- -
    -
  • 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; and
  • -
  • Expression #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.

- -

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 deleted file mode 100644 index 997abe3..0000000 --- a/samples/ternary-operator/style.css +++ /dev/null @@ -1,66 +0,0 @@ -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; -}