当前位置:网站首页>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';
}
输出结果都是一样的,只不过三元运算符看上去更加简洁;
边栏推荐
- The old music player WinAmp released version 5.9 RC1: migrated to VS 2019, completely rebuilt, compatible with Win11
- The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao
- 老牌音乐播放器 WinAmp 发布 5.9 RC1 版:迁移到 VS 2019 完全重建,兼容 Win11
- uni-app中的renderjs使用
- Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query
- Basic Grammar Introduction of Carbon Tutorial (Tutorial)
- 高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
- 高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
- Basic configuration of OSPFv3
- cas and spin locks (is lightweight locks spin locks)
猜你喜欢

Implementation of a sequence table

Payment module implementation

登录业务实现(单点登录+微信扫码+短信服务)

Architecture Battalion Module 8 Homework

MATLAB program design and application 2.4 Common internal functions of MATLAB

Audio alignment using cross-correlation

ThreadLocal

第七章

顺序表的实现

What's wrong with the sql syntax in my sql
随机推荐
Efficient Concurrency: A Detailed Explanation of Synchornized's Lock Optimization
[Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement
NVIDIA已经开始测试AD106和AD107 GPU核心的显卡产品
HTC using official firmware as bottom bag made ROM brush card bag tutorial
Quick Start Tutorial for flyway
Payment module implementation
Redis Overview: Talk to the interviewer all night long about Redis caching, persistence, elimination mechanism, sentinel, and the underlying principles of clusters!...
[Code Hoof Set Novice Village 600 Questions] Leading to the combination of formulas and programs
嵌入式开发没有激情了,正常吗?
Flex layout in detail
C程序设计-方法与实践(清华大学出版社)习题解析
Istio introduction
Verilog implements a divide-by-9 with a duty cycle of 5/18
高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
了解下C# 匿名方法
Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query
Unity-通过预制件和克隆方法动态实现各个UGUI下控件的创建和显示
Carbon教程之 基本语法入门大全 (教程)
A solution to the server encountered an internal error that prevented it from fulfilling this request [easy to understand]
How to debug TestCafe