From 7ff55681e33334f75c19b8cc8ef9fe4dbe0fca05 Mon Sep 17 00:00:00 2001
From: Henry The ternary operator is a special type of conditional operator. A statement using the ternary operator takes the form of: Where: 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: When we run the above code, we get: 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: When we run the above code, we get the same result: 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: When we run the above code, we get: And here's how we would use the ternary operator to condense the above "if-else" statements: When we run the above code, we get the same result:
+
+ 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 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: Note that we can also write the above series of "if-else" statements using a "switch" statement: 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: 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: 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. 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:Ternary Operator
+
+ [Expression #1] ? [Expression #2] : [Expression #3];
+
+
+
+
+ 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.Example #1
+
+
+
+
+
+
+ $age = 20;
+
+ if ($age >= 21) {
+ print "You can drink legally! :)";
+ }
+
+ else {
+ print "Sorry, no beer for you. :(";
+ }
+
+
+ = 21) {
+ print "You can drink legally! :)";
+ }
+
+ else {
+ print "Sorry, no beer for you. :(";
+ }
+ ?>
+
+
+
+
+ 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.
+
+ $age = 20;
+
+ $age >= 21 ? print "You can drink legally! :)" : print "Sorry, no beer for you. :(";
+
+
+ = 21 ? print "You can drink legally! :)" : print "Sorry, no beer for you. :(";
+ ?>
+
+ Example #2
+
+
+
+ $number = -12;
+
+ if ($number >= 0) {
+ $absolute_value = $number;
+ }
+
+ else {
+ $absolute_value = -$number;
+ }
+
+ echo "The absolute value of $number is $absolute_value.";
+
+
+ = 0) {
+ $absolute_value = $number;
+ }
+
+ else {
+ $absolute_value = -$number;
+ }
+
+ echo "The absolute value of $number is $absolute_value.";
+ ?>
+
+
+
+ $number = -12;
+
+ $absolute_value = ($number >= 0) ? $number : -$number;
+
+ echo "The absolute value of $number is $absolute_value.";
+
+
+ = 0) ? $value : -$value;
+
+ echo "The absolute value of $number is $absolute_value.";
+ ?>
+
+
$absolute_value. And rather than using 7 lines of code (counting white space), this was accomplished using only 1 line of code.Example #3
+
+
+
+ $arg = "B";
+
+ if ($arg == "R") {
+ $exercise = "running";
+ }
+
+ elseif ($arg == "B") {
+ $exercise = "biking";
+ }
+
+ elseif ($arg == "S") {
+ $exercise = "swimming";
+ }
+
+ else {
+ $exercise = "walking";
+ }
+
+ echo $exercise;
+
+
+ $arg = "B";
+
+ switch ($arg) {
+ case "R":
+ $exercise = "running";
+ break;
+ case "B":
+ $exercise = "biking";
+ break;
+ case "S":
+ $exercise = "swimming";
+ break;
+ default:
+ $exercise = "walking";
+ }
+
+ echo $exercise;
+
+
+
+
+
+
+ $arg = "B";
+
+ $exercise = ( ($arg == "R") ? "running" :
+ ($arg == "B") ? "biking" :
+ ($arg == "S") ? "swimming" :
+ "walking" );
+
+ echo $exercise;
+
+
+
+
+
+
+ $arg = "B";
+
+ $exercise = $arg == "R" ? "running" :
+ ($arg == "B" ? "biking" :
+ ($arg == "S" ? "swimming" :
+ "walking" ));
+
+ echo $exercise;
+
+
+
+
+
Conclusion
+
+ 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
+
+
+ I had $fruit $food for 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");
+ ?>
+
+
+
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.
- -