The ternary operator is often used in programming. it is also called the "three-object operator". just like his name, he needs three operands, this function selects one of the other two expressions based on one expression, instead of selecting one of the two statements or programs. Let's take a look at the syntax of the ternary operator. Today, we will explain the"
Ternary operators".
Introduction to ternary operators
The ternary operator is often used in programming. it is also called the "three-object operator". just like his name, he needs three operands, this function selects one of the other two expressions based on one expression, instead of selecting one of the two statements or programs. Let's take a look at the syntax of the ternary operator.
Syntax of ternary operators
Use (? :) Format:
Condition? Result 1: Result 2
If the condition is met, select result 1. Otherwise, it is Result 2. we will use an example to describe it later.
The functions of the ternary operators are the same as those of the if... else... flow statements in PHP. However, the ternary operators are written in a row with less code and higher execution efficiency.
Ternary operators
This example uses the ternary operator to implement a simple selection function. if the condition is true, "PHP Chinese network" is output; otherwise, "false" is output. the instance code is as follows:
Code running result:
The role of the ternary operator and the if... else... the process statement is the same, so we use if .... else... the code below is an example of writing a flow statement.
Code running result:
Recommended articles:
1. PHP operator (1) "arithmetic operator" example
2. PHP operator (2) "string operator"
3. PHP operator (3) "value assignment operator" example
4. PHP operator (4) "bit operator" example
5. PHP operator (5) "logical operator" example
6. PHP operator (6) "comparison operator" example
7. PHP operator (7) "error control operator" example
The above is the details of the PHP operator (8) "ternary operator" instance. For more information, see other related articles in the first PHP community!