当前位置:网站首页>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';
}
输出结果都是一样的,只不过三元运算符看上去更加简洁;
边栏推荐
猜你喜欢
嵌入式开发没有激情了,正常吗?
How to debug TestCafe
VOT2021比赛简介
【公开课预告】:超分辨率技术在视频画质增强领域的研究与应用
Audio alignment using cross-correlation
Redis Overview: Talk to the interviewer all night long about Redis caching, persistence, elimination mechanism, sentinel, and the underlying principles of clusters!...
PCB stackup design
The article you worked so hard to write may not be your original
Bika LIMS open source LIMS set - use of SENAITE (detection process)
IDA PRO中汇编结构体识别
随机推荐
How to debug TestCafe
Redis Overview: Talk to the interviewer all night long about Redis caching, persistence, elimination mechanism, sentinel, and the underlying principles of clusters!...
"The core concept of" image classification and target detection in the positive and negative samples and understanding architecture
spark reports an error OutOfMemory "recommended collection"
A few permanent free network transmission, convenient and simple (Intranet through tutorials)
【核心概念】图像分类和目标检测中的正负样本划分以及架构理解
The latest masterpiece!Alibaba just released the interview reference guide (Taishan version), I just brushed it for 29 days
flowable workflow all business concepts
How to get useragent
AI 自动写代码插件 Copilot(副驾驶员)
Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query
[Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement
Payment module implementation
LevelSequence source code analysis
STM32 full series development firmware installation guide under Arduino framework
C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处
C language parsing json string (json object is converted to string)
A solution to the server encountered an internal error that prevented it from fulfilling this request [easy to understand]
支付模块实现
iNeuOS industrial Internet operating system, equipment operation and maintenance business and "low-code" form development tools