当前位置:网站首页>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 .
边栏推荐
- Begin End use the pit encountered
- Simple understanding of the difference between get request and post submission
- Getting started with shell Basics
- MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video
- With the development of industrial Internet, the landing and application of the Internet has become wider
- How does MySQL merge columns?
- Small exercise of process and signal
- 可观测,才可靠:云上自动化运维CloudOps系列沙龙 第一弹
- 想請教一下,我在佛山,到哪裏開戶比較好?手機開戶是安全麼?
- Pytoch viewing model parameter quantity and calculation quantity
猜你喜欢

Introduction to reverse commissioning - VA and RVA conversion in PE 04/07

This article explains the concepts of typed array, arraybuffer, typedarray, DataView, etc

单元测试效率优化:为什么要对程序进行测试?测试有什么好处?

Race of golang

Flat shading with unity

Mysql database foundation: stored procedures and functions

Detailed explanation of the first three passes of upload Labs

Shell programming overview

SQL attendance statistics monthly report

Laravel configures passport and returns token using JWT
随机推荐
Google Earth engine (GEE) -- converts string to number and applies it to time search (ee.date.fromymd)
Comprehensively analyze the basic features and summary of free and paid SSH tools
ot initialized – call ‘refresh’ before invoking lifecycle methods via the context: Root WebApplicati
[Title brushing] avoid flooding
提权扫描工具
Wechat applet realizes map navigation + door-to-door recycling
Logiciel de récupération de données easyrecovery15 téléchargement
(8) JMeter component detailed once only controller
[the path of system analyst] Chapter 5 Software Engineering (Agile Development)
Project management - common English vocabulary I
Data recovery software easyrecovery15 Download
Fastcgi CGI shallow understanding
Laravel RBAC laravel permission use
DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703
QQ was stolen? The reason is
JMeter transaction controller
Directory related commands
Lucky hash quiz system development (source code deployment) fun investment hash game play development (case requirements)
Chapter 13 signal (III) - example demonstration
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