Is there difference between ternary operator and i

2019-05-15 23:22发布

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

if yes, kindly provide.

6条回答
我欲成王,谁敢阻挡
2楼-- · 2019-05-16 00:03

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

查看更多
Summer. ? 凉城
3楼-- · 2019-05-16 00:05

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';
}
查看更多
唯我独甜
4楼-- · 2019-05-16 00:05

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楼-- · 2019-05-16 00:13

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.

查看更多
SAY GOODBYE
6楼-- · 2019-05-16 00:14

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.

查看更多
仙女界的扛把子
7楼-- · 2019-05-16 00:20

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!

查看更多
登录 后发表回答