Is there difference between ternary operator and if condition?

拥有回忆 提交于 2020-01-23 08:43:07

问题


Is there difference between ternary operator and if condition in php ?

if yes, kindly provide.


回答1:


The ternary operator is an operator, so it forms an expression. So it will have a value that you can assign to a variable or use however you want. It is used for simple situations where a variable can take two possible values depending on a condition.

For example: $status = $age > 18 ? 'adult' : 'child';

Although possible, you should not nest the ternary operator.

The control structure if is something absolutely different. The only thing they have in common that both evaluate a condition (true/false). if is used to execute code fragments based on the result of that condition. You will find yourself using if most of the time (in fact, you can live without the ternary). In certain situations, it is less code and more readable if you replace certain types of ifs with the ternary, like this one:

if ($age > 18) {
    $status = 'adult';
} else {
    $status = 'child';
}



回答2:


Personally, I only use the ternary operator if it fits on one line. If it need to span, then it's time for the good old

 $value = ($a < 0) ? 'minus' : 'plus';

also you can see one interesting question how multiple ternary works :

unusual ternary operation




回答3:


If statements are faster than ternary, but in most cases it doesn't matter.

Here is a post on the performance of If statements vs ternary. Summary: It's basically the same unless you are evaluating large objects because ternary copies the objects being evaluated. This was using PHP 5.3 I'm not sure if it has been changed in current versions.




回答4:


The ternary operator can do anything that an if/else statement can. In some cases it can provide brevity and expressiveness, but beware: it is easily abused.

One thing I like it for is checking for null:

$foo = (is_null($bar)) ? 0 : $bar->someNumber();

If my PHP memory serves me correctly, then it can also be used on an lvalue:

((is_null($foo)) ? $bar : $foo) = $quux;

It can be easily overdone though, such as in this C++ example, where it is used in place of loops and if/else statements:

while( ( ! printingStars) ?
     ( ( ! reachedMax) ?
       ( ( ++numberOfStars == n - 1) && (reachedMax = 1) )
       : --numberOfStars ), printingStars = 1, starsLeft = numberOfStars
     : ( ( ! starsLeft ) ?
         printingStars = 0, (std::cout<< std::endl), 1
         : --starsLeft, (std::cout<< "*"), 1 ) );

Use with caution.




回答5:


Maybe another point of view: Performance.

A while ago I noticed that the ternary operator is faster than an if(). I don't know if this still applies to the latest versions of PHP.

< wild guess >
The reason may be that an if() allows more flexibility like using code blocks ({ }). A ternary operator basically is just testing an expression and grabbing a value depending on the result of the test.

Going back into my Assembler-days I'd say that an if() has to jump in memory and, therefore, takes a bit more time to do things.
< /wild guess >

However, this may be become noticeable only if the code is executed a decent number (1k+) of times.




回答6:


There is a big difference in maintenance, if you use none trivial logic.

If you have a difficult problem you need under circumstances much more time, to sove it, as if you have an 'if ... then' in your code. And be sure: it's happen!

The time is not a friend of ternary operator. Fast to write, but not fast to understand, if the years gone!



来源:https://stackoverflow.com/questions/5989982/is-there-difference-between-ternary-operator-and-if-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!