Home Backend Development PHP Tutorial PHP Operator (8) 'Ternary Operator' Example Explanation

PHP Operator (8) 'Ternary Operator' Example Explanation

Apr 12, 2017 pm 03:11 PM

Today I will explain to you the "ternary operator" among PHP operators.

Introduction to the ternary operator

The ternary operator is often used in programming. It is also called the "ternary operator", just like its name. , it requires three operands, and its function is to select one of two expressions based on one expression, rather than selecting one between two statements or programs. Let’s take a look at the syntax of the ternary operator.

The syntax of the ternary operator

The ternary operator is represented by (?:) and is written as follows

条件?结果1:结果2
Copy after login

When the condition is met, select Result 1, otherwise it is result 2. We will use examples to illustrate it later.

PHP Operator (8) Ternary Operator Example ExplanationThe ternary operator has the same function as the if...else...process statement in PHP. However, the ternary operator is written in one line, with less code and high execution efficiency. a little.

Ternary Operator Example

This example uses the ternary operator to implement a simple selection function. If the condition is true, "PHP Chinese Network" will be output, otherwise it will be output "false", the example code is as follows:

<?php
header("content-type:text/html;charset=utf-8");   //设置编码
$a=100;                                           //说明一个变量
$b=($a==true?PHP中文网:false);
echo $b;
?>
Copy after login

Code running result:

PHP Operator (8) Ternary Operator Example Explanation

Above we talked about the role of the ternary operator and the if in PHP. ..else... process statements are the same, then we use if...else... process statements to write the above example again. The code is as follows

<?php
header("content-type:text/html;charset=utf-8"); //设置编码
$a=100;                                         
if($a==true){
    echo "PHP中文网";
}else{
    echo "false";
}
?>
Copy after login

Code running results:

PHP Operator (8) Ternary Operator Example Explanation

You can see that the results of the two examples are the same.

PS: Although the ternary operator is the same as the if...else...process statement, in most cases we only use the ternary operator when the code is relatively simple.

The above is a simple application of the ternary operator

Recommended related articles:

1.PHP Operator (1) "Arithmetic Operator" Example explanation

2.PHP operator (2) Detailed explanation of "String operator" example

3.PHP operator (3) "Assignment operator" example explanation

4.PHP operator (4) "Bit operator" example explanation

5.PHP operation Operator (5) "Logical operator" example explanation

6.PHP operator (6) "Comparison operator" example explanation

7.PHP Operator (7) "Error Control Operator" Example Example

The above is the detailed content of PHP Operator (8) 'Ternary Operator' Example Explanation. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
What is the root operator in C language? What is the root operator in C language? Mar 06, 2023 pm 02:39 PM

In the C language, there is no root operator. The built-in function "sqrt()" is used to open the root, and the syntax "sqrt(value x)" is used; for example, "sqrt(4)" is to perform the square root operation on 4. , the result is 2. sqrt() is a built-in root operation function in C language. Its operation result is the arithmetic square root of the function variable; this function can neither operate negative values ​​nor output imaginary results.

Golang error: 'invalid use of ... operator' How to solve it? Golang error: 'invalid use of ... operator' How to solve it? Jun 24, 2023 pm 05:54 PM

For Golang developers, "invaliduseof...operator" is a common error. This error usually occurs when using variable-length parameter functions. It will be detected at compile time and indicate which parts have problems. This article will introduce how to solve this error. 1. What is a variable-length parameter function? A variable-length parameter function is also called a variable-parameter function. It is a function type in the Golang language. Using variable-length parameter functions, you can define multiple ones as follows

What is the meaning of '==' symbol in php What is the meaning of '==' symbol in php Mar 14, 2023 pm 07:05 PM

In PHP, the "==" symbol is a comparison operator that can compare whether two operands are equal. The syntax is "operand 1 == operand 2". The "==" operator compares and tests whether the variable on the left (expression or constant) has the same value as the variable on the right (expression or constant); it only compares the values ​​of the variables, not the data types. If the two values ​​are the same, it returns a true value; if the two values ​​are not the same, it returns a false value.

What does % mean in Java What does % mean in Java Mar 06, 2023 pm 04:48 PM

In Java, "%" means remainder. It is a binary arithmetic operator that can perform division operations and obtain the remainder. The syntax is "operand 1 % operand 2". The operand of the remainder operator "%" is usually a positive integer or a negative number or even a floating point number. If a negative number participates in this operation, the result depends on whether the previous number is positive or negative.

Analysis of the meaning and usage of += operator in C language Analysis of the meaning and usage of += operator in C language Apr 03, 2024 pm 02:27 PM

The += operator is used to add the value of the left operand to the value of the right operand and assign the result to the left operand. It is suitable for numeric types and the left operand must be writable.

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

How to determine if two numbers are divisible in php How to determine if two numbers are divisible in php Jan 10, 2023 pm 03:12 PM

In PHP, you can use the "%" and "==" operators to determine whether two numbers are divisible; you only need to use the "%" operator to divide the two numbers to get the remainder, and then use the "==" operator Just judge whether the obtained remainder is 0. The syntax is "Number 1 % Number 2 == 0". If it is 0, it can be divisible. If it is not 0, it cannot be divisible.

Python Operators: The Ultimate Guide from Newbie to Master Python Operators: The Ultimate Guide from Newbie to Master Mar 11, 2024 am 09:13 AM

Introduction to python operators Operators are special symbols or keywords used to perform operations between two or more operands. Python provides a variety of operators covering a wide range of uses, from basic mathematical operations to complex data manipulation. Mathematical operators Mathematical operators are used to perform common mathematical operations. They include: operator operation examples + addition a + b - subtraction a-b * multiplication a * b / division a / b % modulo operation (take the remainder) a % b ** power operation a ** b // integer division (discard the remainder) a//b Logical Operators Logical operators are used to concatenate Boolean values ​​and evaluate conditions. They include: operator operations examples and logical and aandbor logical or aorbnot logical not nota comparison operations

See all articles