Is it possible to have a ternary without the else block?

-1

Is there a possibility to create a simpler if ternary, say without the else block code? how would it look?

Type:

if($total < 10){
 $total_gerente = 10;
}

For what I know the ternary has IF and ELSE. Is it possible to only have the if block?

    
asked by anonymous 11.07.2016 / 15:06

2 answers

3

Translating the question code into a ternary condition:

$total_gerente = (($total < 10)? 10: null);

Recommended to delimit the condition with parentheses to avoid problems with syntax errors.

Alternatively, you could do so if you did not want to set a specific value in the opposite condition

($total < 10)? $total_gerente = 10: null;

Note:

You should be aware that actions should not have more than one line.

Error code example

($total < 10)? $total_gerente = 10; $outra_var = 'foo': null;

When there is such a need you can use techniques with anonymous functions, but as this is not part of the question, I abstain and go deeper into the subject.

    
11.07.2016 / 15:27
0

You can make it simpler in some ways: 1st

if($total < 10) $total_gerente = 10;

2nd

  if($total < 10) :
      $total_gerente = 10;
  endif;

3rd

if($total < 10) 
    $total_gerente = 10;

I hope I have helped.

    
11.07.2016 / 15:17