当前位置:网站首页>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';
}
输出结果都是一样的,只不过三元运算符看上去更加简洁;
边栏推荐
- BM5 合并k个已排序的链表
- C程序设计-方法与实践(清华大学出版社)习题解析
- Collation of knowledge points in Ningbo University NBU IT project management final exam
- useragent online lookup
- Audio alignment using cross-correlation
- sqlite3 simple operation
- Redis综述篇:与面试官彻夜长谈Redis缓存、持久化、淘汰机制、哨兵、集群底层原理!...
- Memblaze发布首款基于长存颗粒的企业级SSD,背后有何新价值?
- BOW/DOM (top)
- Chapter VII
猜你喜欢
AI 自动写代码插件 Copilot(副驾驶员)
Payment module implementation
Flex layout in detail
Redis综述篇:与面试官彻夜长谈Redis缓存、持久化、淘汰机制、哨兵、集群底层原理!...
[Intensive reading of the paper] iNeRF
flowable workflow all business concepts
Memblaze发布首款基于长存颗粒的企业级SSD,背后有何新价值?
Realize serial port receiving data based on STM32 ring queue
高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
Collation of knowledge points in Ningbo University NBU IT project management final exam
随机推荐
Unity - LineRenderer show a line
LevelSequence source code analysis
二叉树非递归遍历
Linux environment redis cluster to build "recommended collection"
server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none failed
C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处
Memblaze发布首款基于长存颗粒的企业级SSD,背后有何新价值?
求n以内的素数
统计UTF-8字符串中的字符函数
老牌音乐播放器 WinAmp 发布 5.9 RC1 版:迁移到 VS 2019 完全重建,兼容 Win11
架构实战营模块八作业
AI 自动写代码插件 Copilot(副驾驶员)
Verilog implements a divide-by-9 with a duty cycle of 5/18
Write a database document management tool based on WPF repeating the wheel (1)
"The core concept of" image classification and target detection in the positive and negative samples and understanding architecture
Thymeleaf是什么?该如何使用。
The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao
focus on!Haitai Fangyuan joins the "Personal Information Protection Self-discipline Convention"
BM3 将链表中的节点每k个一组翻转
Pytest初体验