In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "php ternary operator related knowledge summary". In daily operation, I believe that many people have doubts about php ternary operator related knowledge summary. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about php ternary operator related knowledge summary! Next, please follow the editor to study!
Today, when I was correcting the paper online, I came across a sentence that I couldn't understand:
$if_summary = $row ['IF_SUMMARY'] = = 2percent' Yes':'no'
Later, Baidu found out that it was the ternary operator of PHP.
This sentence is tantamount to
If ($row ['IF_SUMMARY'] = = 2) {$if_summary= "Yes";} else {$if_summary= "No";}
The function of the ternary operator is the same as the "if...else" process statement, it is written on one line, the code is very concise, and the execution is more efficient.
Proper use of ternary operators in PHP programs can make scripts more concise and efficient.
The code format is as follows: (expr1)? (expr2): (expr3)
Explanation: if the condition "expr1" is true, execute the statement "expr2", otherwise execute "expr3".
To achieve the same function, if you use conditional flow statements, you need to write multiple lines of code:
If (expr1) {expr2;} else {expr3;}
It can be seen that the good ternary operator mentioned above is not an exaggeration. In most cases, however, we only use the ternary operator when the code is simple, that is, when the execution statement is only a single sentence. Such as:
$a > $b? Print "an is greater than b": print "an is less than b"
In fact, the ternary operator can be extended to execute more than one statement when the set condition is true or not. Try the following format:
(expr1)? (expr2). (expr3): (expr4). (expr5)
It is very obvious that multiple execution statements can be concatenated with string operation symbols ("."), and each execution statement is surrounded by small angle brackets to indicate that it is a separate and complete execution statement. After this expansion, its function is more similar to the "if...else" process statement.
At the same time, ternary operators can also be used nested. For example, if an is greater than b, then x=c-an is not x=b-c if an is less than c; otherwise an is less than b if it is true: if b is less than c, then x=b-c is not true:
$a > $b? $x = ($aqb?': print 'No'
It should be noted that when using ternary operators, it is recommended to use print statements instead of echo statements.
Note the understanding of the following series of sentences:
$str = $_ GET ['abc']? 'wangjinbo': 'wjb'
This cannot be understood as: when $str is equal to $_ GET ['abc'], the assignment is' wangjinbo' otherwise assigned to 'wjb'; because one: to judge equality should use = =; the syntax of the original two: ternary operator is as shown above: (expr1)? (expr2): (expr3), obviously the above binary, ternary 'wangjinbo'' or 'wjb'' cannot constitute a meaningful expression alone.
The correct understanding is: when $_ GET ['abc'] is empty (that is, whether in PHP,'', null,0,undifine, all equivalent Boolean values false), assign $str to 'wangjinbo', otherwise assign to' wjb'
At this point, the study of "php ternary operator related knowledge summary" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.