当前位置:网站首页>PHP三元(三目)运算符
PHP三元(三目)运算符
2022-07-31 22:14:00 【小苗吃不够】
PHP三元(三目)运算符
语法格式:
(expr1) ? (expr2) : (expr3)
当expr1为true时(成立),返回值为expr2,当expr1为false时(不成立),返回值为expr3。
不理解继续向下看:
//三元运算符写法:
$a = 1;
echo !empty($a) ? $a : 'null'; //!empty($a)是true输出$a ,false输出'null'
//if写法:
$a = 1;
if(!empty($a)){
echo $a;
}else{
echo 'null';
}
上面两个表达式是一致的。
从PHP 5.3 起,可以省略三元运算符中间那部分。表达式 expr1 ?: expr3 ,当expr1为true时(成立)返回 expr1,否则返回 expr3。
//三元运算符写法:
$a = 1;
echo $a ?: 'null'; //$a是true输出$a , false输出'null'
//if写法
$a = 1;
if(!empty($a)){
echo $a;
}else{
echo 'null';
}
输出结果都是一样的,只不过三元运算符看上去更加简洁;
边栏推荐
- Verilog implements a divide-by-9 with a duty cycle of 5/18
- BM5 合并k个已排序的链表
- renderjs usage in uni-app
- 「SDOI2016」征途 题解
- BM3 flips the nodes in the linked list in groups of k
- A solution to the server encountered an internal error that prevented it from fulfilling this request [easy to understand]
- ReentrantLock原理(未完待续)
- Document management and tools in the development process
- 【论文精读】iNeRF
- Learn about C# anonymous methods
猜你喜欢

Bionic caterpillar robot source code
Dry goods | 10 tips for MySQL add, delete, change query performance optimization

Write a database document management tool based on WPF repeating the wheel (1)

Unity-LineRenderer显示一条线

Document management and tools in the development process

Arduino框架下STM32全系列开发固件安装指南

ECCV 2022 Huake & ETH propose OSFormer, the first one-stage Transformer framework for camouflaging instance segmentation!The code is open source!...

Count characters in UTF-8 string function

What's wrong with the sql syntax in my sql

利用反射实现一个管理对象信息的简单框架
随机推荐
Several methods of mysql backup table
财务盈利、偿债能力指标
高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
How to identify fake reptiles?
「SDOI2016」征途 题解
grep command written test questions
C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处
Recognize anomalies (you will understand after reading this)
iNeuOS industrial Internet operating system, equipment operation and maintenance business and "low-code" form development tools
uniapp小程序检查、提示更新
[QNX Hypervisor 2.2用户手册]9.14 set
「APIO2010」巡逻 题解
Federated Learning: Multi-source Knowledge Graph Embedding in Federated Scenarios
BM5 merge k sorted linked lists
Redis Overview: Talk to the interviewer all night long about Redis caching, persistence, elimination mechanism, sentinel, and the underlying principles of clusters!...
Golang must know the Go Mod command
Returns a zero-length array or empty collection, do not return null
Student management system on the first day: complete login PyQt5 + MySQL5.8 exit the operation logic
hboot与recovery、boot.img、system.img
#yyds dry goods inventory# Interview must brush TOP101: the entry node of the ring in the linked list