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

Asked

Viewed 3,050 times

0

There is the possibility of creating a simpler if ternary, I say without the code of the Else block? how would it look?

guy :

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

Because from what I know the ternary has IF and ELSE. There is possibility of only having the if block?

  • 2

    You can’t understand what you want. There’s nothing ternary about it. Explain it better. Explain calmly, think about the problem you want to solve. Try to give enough information to people who don’t know what you want to make them understand what you want.

  • 2

    In the ternary if you do not want the Else part, return/leave an empty string or null.

  • I don’t want the ELSE , but I don’t want to create the variable!

  • 3

    I can’t understand it yet, maybe putting more parts of the code in to give a better context would help. Or put it how you want it to be, even if in the wrong way.

  • 1

    You need the Else part to satisfy the syntax.

  • You really need to know more about your code, more than I can understand I believe what you want is $total_gerente = $total < 10 ? 10 : $total;, in that case the $total is less than 10 assigns 10 in the variable $total_gerente otherwise assigns the variable value $total

Show 1 more comment

2 answers

3

Translating the question code for a ternary condition:

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

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

Alternatively, you can 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 have no more than one line.

Example of code that generates error

($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 deepen more on the subject.

0

You can make it simpler in some ways:
first

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

2nd

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

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

I hope I’ve helped.

  • 3

    What this has changed?

  • went from 3 to 1 line

  • 3

    Piling things up doesn’t make anything simpler. His original was even presenting in a line

  • In the initial code keys are opened as if it would have several rules within the if. as it will only assign a value to the variable, for sure this way becomes simpler.

  • 1

    Simplicity is something else, it just saved typing.

  • 2

    But it still doesn’t justify having made an answer to it. And anyway it doesn’t solve anything. The question remains illogical and the only answer too.

  • 1

    3 options are not ternary Ifs. They are alternative syntaxes. Note that the if ternary does not explicitly state the term if()

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.