Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction and usage of the & quot;?: & quot; operator in PHP

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Many novices are not very clear about the introduction and usage of the "?:" operator in PHP. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

In PHP? And: what are the operators like?

As follows:

(($request_type = = 'SSL')? HTTPS_SERVER: HTTP_SERVER)

It is a conditional operator.

$x? $y: $z

If $x is true, use $y; if not, use $z.

It can also be simplified.

$x?: $z

If $x is true, use $x; if not, use $z.

Some people will say that it is a "ternary operator". This is not right. If it is a ternary operator, it should have three operands. This is thought because it is usually the only ternary operator in the current language.

The following will describe what ternary is, how to use it, when to use it, why to use it, and when it can't be used.

What is a ternary operator?

?: is the abbreviation of if and else. There is an official explanation of the "ternary operator" in the middle.

Since PHP 5.3:

Starting with PHP 5.3, you can omit the middle part of the ternary operator. The expression expr1?: expr3 returns expr1 if expr1 is TRUE, expr3 otherwise.

Since PHP 7.0,

PHP 7 has a new Null merge operator. Like ternary operations, this is also called the "isset ternary operator". This removes isset () from the chained ternary.

In PHP 5, if you use the ternary operator on a variable that may not exist, you must use isset () at the beginning of the ternary statement:

$result = isset ($nonExistentVariable)? $nonExistentVariable: 'default'

In PHP 7, you can do this:

$result = $nonExistentVariable? 'default'

Remember that the Null merge operator cannot handle empty strings. You can use it in chains to check multiple variables:

$user = $userImpersonatingAnotherUser? $loggedInUser? "Guest"

In PHP, it is not uncommon for administrators to impersonate users to test loggable systems. As above, if the user does not pretend to be another user and does not log in, he will become a visitor. If you don't understand, read on to learn what the ternary operator is and how to use it, and then look back at the new PHP

How do I use the ternary operator?

The normal if statement is as follows:

If (isset ($_ POST ['hello'])) {$var =' exists';} else {$var = 'error';}

Simplify using ternary operators.

$var = isset ($_ POST ['hello'])? 'exists':' error'; ^ ^ | | then | else | if post isset $var=this $var=this

Shorter, but difficult to understand. This can be used not only on the variable $var as above, but also in echo as follows to check whether the variable is false:

$isWinner = false;// output 'you lose'echo ($isWinner)?' You windings': 'You lose';// ditto return ($isWinner)? 'You winnings': 'You lose'

Why use them?

I think ternary operation is fascinating. Some developers want to perform, but ternary operators are good, especially when combined with other features, such as PHP 5.4's latest short echos.

Slightly beside the point, in 'view/template' (note the MVC example), if you want to use a little server-side logic, it would be best to use ternary operators or other short sentences. "other short sentences", like this:

If ($isWinner): / / Show something coolendif

Note that I personally don't like such meaningless short sentences.

How fast is the ternary operator?

People like micro-optimization. It's the same thing. So it's important to know how much faster ternary operations are than normal if / else statements.

From the article, I know about fast 0.5ms. This is much faster!

Wait, that's not true. This conclusion stems from repeated thousands of operations in a single line of code. So don't worry at all, it's pointless.

Impractical ternary operator

The code should:

Easy to read

Easy to understand

Easy to modify

Obviously, it depends on the coding ability of the person looking at the code and the ability to understand such problems. The previous simple example is fine, but the following is not good:

Echo ($colour = = 'red')? "Omg we're going to die": ($colour = = 'blue'? "Ah sunshine and daisies": ($colour = = 'green'? "Trees are green": "The bloody colour is orange, isn't it? That was pointless.")

The reasons are as follows:

Ternary nesting is too long

You can use the switch statement

The first one should be orange.

Ternary operation is actually very simple, there is no need to be afraid. Don't think that there is any speed increase, it really doesn't make any difference. Use it when you can make the result simple and look good, and always keep the code readable. Do not use ternary operators without ternary.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report