Home > Article > Backend Development > Introduction to ternary operator
1. What is ternary arithmetic: (Boolean expression? Value 0:Value 1;)
5>3 ?alert('5 big'):alert('3 big');
that is if(5>3){alert('5 big')}else{alert('3 big')};
Note: The difference between ternary operation and if(){}else{} is that ternary operation has a return value
For example:
var max = a>b? a:b;
2. How to write multi-condition ternary operation:
Example: Determine the four levels of ABCD based on student scores
var result = (sc<0 || sc>100) ?("Invalid score"):
sc>=90?("A"):
sc>=80?("B"):
sc>=60?("C"):("D");
Note: Priority issues need to be considered when calculating, add "()' to avoid wrong results!
3. Advantages of ternary operation: concise and clear
name = value 1 if condition else value 2
## Deep copy and shallow copy
str creates a value that cannot be modified. If modified, create a Function
The above is the detailed content of Introduction to ternary operator. For more information, please follow other related articles on the PHP Chinese website!