Detailed explanation of php ternary operator examples

墨辰丷
Release: 2023-03-28 11:10:01
Original
5086 people have browsed it

The ternary operator in php is also called the ternary operator. In fact, I often call it the question mark operator. In fact, it can be done like this. The ternary operator can implement simple conditional judgment functions. Let me explain. Let me introduce some examples of ternary operators

Recommended manual:php complete self-study manual

The functions of the ternary operator are the same as "if ....else" process statement is consistent, it is written in one line, the code is concise and the execution efficiency is high. Proper use of the ternary operator in PHP programs can make scripts more concise and efficient. The syntax of the code is as follows:

(expr1)?(expr2):(expr3); //表达式1?表达式2:表达式3
Copy after login

Explanation:

If the condition "expr1" is true, execute the statement "expr2", otherwise execute "expr3".

<?PHP
$a=10; $b=20;
$c=$a>$b?($a-$b):($a+$b);
//说明:如果变量a大于变量b则执行问号后面的,否则就执行:冒号后面的
echo $c;
?>
Copy after login

Expressions can be functions, arrays, etc.

Recommended related articles:
1.PHP ternary operator: fast or not?
2.Simple comparison of ternary operator and Null coalescing operator in PHP
3.What are the common operators in php
Related video recommendations:
1.Dugu Jiujian (4)_PHP video tutorial

In fact, the ternary operator can Extended use, when the set conditions are true or not, the execution statement can be more than one sentence. Try the following format:

(expr1) ? (expr2).(expr3) : (expr4).(expr5);
Copy after login

We can clearly see that multiple execution statements can use string operation symbols (". ") are connected, and each execution statement is surrounded by small angle brackets to indicate that it is an independent and complete execution statement. After this expansion, its function is closer to the "if...else" process statement.

At the same time, the ternary operator can also be used nested. For example, when a is greater than b: if a is less than c, then x=c-a otherwise x=a-c; otherwise when a is less than b: if b is less than c, then x=c-b otherwise x=b-c:

$a>$b ? $x=($a<$c ? $c-$a : $a-$c) : $x=($b<$c ? $c-$b : $b-$c);
Copy after login

The readability of the nested ternary operator is not very good, and there may be problems with maintaining the code in the future, so in this case we should just use if else if to implement it.

Thank you for reading, I hope it can help you, thank you for your support of this site! The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php Introduction to the use of ternary operator, php operator

php ternary operator Detailed explanation, detailed explanation of operators

Usage examples of PHP ternary/ternary operators

The above is the detailed content of Detailed explanation of php ternary operator examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template