PHP operator (8) "ternary operator" example

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.