当前位置:网站首页>PHP conditional operator
PHP conditional operator
2022-06-30 14:16:00 【Huaxi GG】
Ternary operator syntax : Conditions ? result 1 : result 2 explain : The position in front of the question mark is the condition of judgment , If the conditions are met, the result is 1, Results when not satisfied 2. Let's discuss it in detail .
When I was changing my paper online today, I encountered a sentence that I couldn't understand :
$if_summary = r o w [ ′ I F S U M M A R Y ′ ] = = 2 ? ′ yes ′ : ′ no ′ ; after Come on hundred degree after Hair present yes P H P Of 3、 ... and element shipment count operator this sentence word Of It means thinking Just etc. On yes i f ( row['IF_SUMMARY']==2?' yes ':' no '; Later, baidu found that PHP The ternary operator of The meaning of this sentence is if( row[′IFSUMMARY′]==2?′ yes ′:′ no ′; after Come on hundred degree after Hair present yes PHP Of 3、 ... and element shipment count operator this sentence word Of It means thinking Just etc. On yes if(row[‘IF_SUMMARY’]==2){
$if_summary=“ yes ”;
}else{
$if_summary=“ no ”;
}
The functions of ternary operators are the same as “if…else” Consistent process statements , It's written in one line , The code is very concise 、 More efficient execution .
stay PHP Proper use of ternary operators in the program can make the script more concise 、 Efficient .
The code format is as follows :(expr1) ? (expr2) : (expr3);
explain : If the condition “expr1” establish , Then execute the statement “expr2”, Otherwise execution “expr3”.
Implement the same functionality , If you use conditional flow statements , You need to write multiple lines of code :
if(expr1) {
expr2;
} else {
expr3;
}
so , The advantages of the ternary operators mentioned above are not exaggerated . But , In most cases, we only use ternary operators when the code is relatively simple , That is, when the execution statement is only a single sentence . Such as :
a > a> a>b ? print “a Greater than b” : print “a Less than b”;
in fact , Ternary operators can be extended to , When the set conditions are true or false , The execution statement can be more than one , Try the following format :
(expr1) ? (expr2).(expr3) : (expr4).(expr5);
We see very clearly , Multiple execution statements can use string operation symbols (“.”) Connect , Each execution statement is enclosed by small angle brackets to indicate that it is an independent and complete execution statement . After this expansion, its function is closer to “if…else” Process statement .
At the same time, ternary operators can also be nested . for example ,a Greater than b When established : If a Less than c, that x=c-a otherwise x=a-c; otherwise a Less than b When established : If b Less than c, that x=c-b otherwise x=b-c:
a > a> a>b ? x = ( x=( x=(a<$c ? c − c- c−a : a − a- a−c) : x = ( x=( x=(b<$c ? c − c- c−b : b − b- b−c);
The readability of ternary operators used in nesting is not very good , Maintenance of the code in the future is likely to be problematic , But compared with “if…else” Process statements like , Under the above circumstances , It's really too concise , This is what makes it attractive .
For people who like to be lazy and pursue simple code , Replace with ternary operator if Process statements should be a great choice . Even without considering “ Three yuan ” Any one except the conditional sentence in “ element ”, Using the ternary operator is still better than if Concise statement . The syntax of the following statement is correct , They omit the second or third... In a way that deciphers the quotation marks “ element ”:
a > a> a>b ? print “Yes” : “”;
a > a> a>b ? ‘’: print ‘No’;
It should be noted that : When using ternary operators , It is recommended to use print Statement substitution echo sentence .
Pay attention to the understanding of the following sentences :
?
1
$str = G E T [ ′ a b c ′ ] ? ′ w a n g j i n b o ′ : ′ w j b ′ ; this in No can The reason is Explain by : When _GET['abc'] ? 'wangjinbo' : 'wjb'; It cannot be understood here as : When GET[′abc′]?′wangjinbo′:′wjb′; this in No can The reason is Explain by : When str be equal to G E T [ ′ a b c ′ ] when , Fu value by ′ w a n g j i n b o ′ no be Fu value by ′ w j b ′ ; because by One : sentence break phase etc. Should be The use = = ; because primary Two : 3、 ... and element count operator Of language Law yes Such as On the in : ( e x p r 1 ) ? ( e x p r 2 ) : ( e x p r 3 ) , display however On Noodles Of Two element , 3、 ... and element ′ w a n g j i n b o ′ or ′ w j b ′ No can single single structure become One individual Yes It means The righteous Of surface reach type ; just indeed The reason is Explain yes : When _GET['abc'] when , The assignment is 'wangjinbo' Otherwise, it is assigned as 'wjb'; Because one : To judge equality, we should use ==; Yinyuan II : The syntax of the ternary operator is as shown above :(expr1) ? (expr2) : (expr3), Obviously the above binary , Three yuan 'wangjinbo' or 'wjb' Can't form a meaningful expression alone ; Correct understanding is : When GET[′abc′] when , Fu value by ′wangjinbo′ no be Fu value by ′wjb′; because by One : sentence break phase etc. Should be The use ==; because primary Two : 3、 ... and element count operator Of language Law yes Such as On the in :(expr1)?(expr2):(expr3), display however On Noodles Of Two element , 3、 ... and element ′wangjinbo′ or ′wjb′ No can single single structure become One individual Yes It means The righteous Of surface reach type ; just indeed The reason is Explain yes : When _GET[‘abc’] It's empty ( Whether or not ,PHP in ‘’,null,0,undifine, Are equivalent to Boolean values false) when , take $str The assignment is ’wangjinbo’, Otherwise, it is assigned as ’wjb’;
The above is the whole content of this article , I hope you like it .
边栏推荐
- Knowledge dissemination cannot replace professional learning!
- QQ was stolen? The reason is
- “即服务”,企业数字化转型的必然选择
- Google Earth Engine(GEE)——将字符串的转化为数字并且应用于时间搜索( ee.Date.fromYMD)
- 智慧运维:基于 BIM 技术的可视化管理系统
- When SQL queries are performed in table storage, an error is reported when the primary key is added to the query result, and the query result exceeds 10W rows. Do you want to add multiple indexes to t
- MySQL access denied, opened as Administrator
- Numpy creates an empty array data = np empty(shape=[1, 64,64,3])
- Geoffreyhinton: my 50 years of in-depth study and Research on mental skills
- "As a service", the inevitable choice of enterprise digital transformation
猜你喜欢
【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
用Unity实现Flat Shading
【科研数据处理】[实践]类别变量频数分析图表、数值变量分布图表与正态性检验(包含对数正态)
智慧运维:基于 BIM 技术的可视化管理系统
【刷题篇】避免洪水泛滥
科普达人丨漫画图解什么是eRDMA?
【观察】智能产业加速,为何AI算力要先行?
【科学文献计量】外文文献及中文文献关键词的挖掘与可视化
Intelligent operation and maintenance: visual management system based on BIM Technology
Google Earth Engine(GEE)——GHSL:全球人类住区层,建成网格 1975-1990-2000-2015 (P2016) 数据集
随机推荐
Data recovery software easyrecovery15 Download
[Title brushing] heater
Talk about Vue's two terminal diff algorithm, analysis of the rendering principle of the mobile terminal, and whether the database primary key must be self incremented? What scenarios do not suggest s
This editor will open source soon!
Problems in QT creator (additional unknown and error lines are listed in the debug output window)
When SQL queries are performed in table storage, an error is reported when the primary key is added to the query result, and the query result exceeds 10W rows. Do you want to add multiple indexes to t
科普达人丨漫画图解什么是eRDMA?
Observable, seulement fiable: première bombe de salon de la série cloudops d'exploitation et d'entretien automatisés dans le nuage
深入理解.Net中的线程同步之构造模式(二)内核模式3.内核模式构造物Mutex
[the path of system analyst] Chapter 5 Software Engineering (Agile Development)
[observation] as the intelligent industry accelerates, why should AI computing power take the lead?
MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video
Lucky hash quiz system development (source code deployment) fun investment hash game play development (case requirements)
More than 20 years after Hong Kong's return, Tupu digital twin Hong Kong Zhuhai Macao Bridge has shocked
org.json.JSONObject对象转json,json新增元素,根据json的key获取值。以及list对象格式字符串转jsonArray
This article explains the concepts of typed array, arraybuffer, typedarray, DataView, etc
Deep understanding Net (2) kernel mode 3 Kernel mode construct mutex
IM即时通讯应用开发中无法解决的“顽疾”
Complete TCP forwarding server (kernel linked list + mutex)
Google Earth engine (GEE) - ghsl: global human settlements layer, built grid 1975-1990-2000-2015 (p2016) data set