Is there a difference between a ternary operator and a condition?

Difference between ternary operator and condition in php?

if so, kindly provide.

+3
source share
6 answers

The ternary operator is an operator , therefore, it forms an expression. That way, it will have a value that you can assign to the variable or use as you want. It is used for simple situations when a variable can take two possible values ​​depending on the condition.

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

Although it is possible you should not embed a ternary operator .

if - . , , (true/false). if . if (, ). , if , :

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

, . ,

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

, :

+1

, , .

- If vs trernary. . , , . PHP 5.3. , .

+1

, if/else. , : .

, , null:

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

PHP , lvalue:

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

, , ++, if/else:

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 ) );

.

0

, : .

, , if(). , PHP.

< a >
, if() , , ({ }). .

, , if() , , , - .
</wild guess >

, (1k +) .

0

There is a big difference in maintenance unless you use trivial logic.

If you have a difficult problem, you need to do it in much longer cases, as if you have an "if ... then" in your code. And be sure: it will happen!

Time is not a friend of the ternary operator. Write fast, but don’t quickly understand if the years are gone!

-1
source

All Articles