当前位置:网站首页>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';
}
输出结果都是一样的,只不过三元运算符看上去更加简洁;
边栏推荐
- #yyds dry goods inventory# Interview must brush TOP101: the entry node of the ring in the linked list
- Dry goods | 10 tips for MySQL add, delete, change query performance optimization
- 嵌入式开发没有激情了,正常吗?
- Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query
- Getting Started with Tkinter
- 统计UTF-8字符串中的字符函数
- sqlite3 simple operation
- server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none failed
- 高效并发:Synchornized的锁优化详解
- What's wrong with the sql syntax in my sql
猜你喜欢

iNeuOS industrial Internet operating system, equipment operation and maintenance business and "low-code" form development tools

ReentrantLock原理(未完待续)

useragent online lookup

Student management system on the first day: complete login PyQt5 + MySQL5.8 exit the operation logic

Redis综述篇:与面试官彻夜长谈Redis缓存、持久化、淘汰机制、哨兵、集群底层原理!...

Realization of character makeup

Redis Overview: Talk to the interviewer all night long about Redis caching, persistence, elimination mechanism, sentinel, and the underlying principles of clusters!...

Realize serial port receiving data based on STM32 ring queue

focus on!Haitai Fangyuan joins the "Personal Information Protection Self-discipline Convention"

Document management and tools in the development process
随机推荐
pytorch lstm时间序列预测问题踩坑「建议收藏」
了解下C# 匿名方法
Flink_CDC construction and simple use
C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处
如何减少软件设计和实现之间鸿沟
Efficient Concurrency: A Detailed Explanation of Synchornized's Lock Optimization
Niuke.com brush questions (1)
每月一书(202207):《Swift编程权威指南》
Financial profitability and solvency indicators
Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query
「SDOI2016」征途 题解
Shell script quick start to actual combat -02
The latest masterpiece!Alibaba just released the interview reference guide (Taishan version), I just brushed it for 29 days
BM5 合并k个已排序的链表
linux查看redis版本命令(linux查看mysql版本号)
The principle of ReentrantLock (to be continued)
Pytorch lstm time series prediction problem stepping on the pit "recommended collection"
Implementing a Simple Framework for Managing Object Information Using Reflection
A shortcut to search for specific character content in idea
ThreadLocal