0

I'm trying to simplify this control block:

if (!isset($mobileNumberHF['IS_VALID']) && $mobileNumberHF['IS_VALID'] != 0) {
  echo '<strong class="text-warning">Sin validar</strong>';
} else if ($mobileNumberHF['IS_VALID'] == 0) {
  echo '<strong class="text-danger">Inválido</strong>';
} else {
  echo '<strong class="text-success">Validado</strong>';
}

Like this:

$isValidMobileNum = $mobileNumHF['IS_VALID'];
$mobileNumStatusLabel = ($isValidMobileNum == 1) ? 'Valid' : (!isset($isValidMobileNum)) ? 'Hasn't been validated' : 'Invalid';

The test scenarios are when $isValidMobileNum equals NULL, 0 or 1;

By now, the result varies in a way I'm still trying to understand. Sometimes the output is 'Valid', sometimes 'Invalid' and sometimes 'Hasn't been validated'

For example, for $isValidMobileNum = 1 I'm getting Invalid?

3
  • The above control block looks simplified enough. Easier to read too. Commented Apr 1, 2016 at 21:35
  • Yes, but I'm using them for mobile, house and other phone numbers. I also want to understand what's happening with the ternary logic. I updated the post with an example of what I'm getting right now. @CharlotteDunois Commented Apr 1, 2016 at 21:37
  • 1
    !isset($mobileNumberHF['IS_VALID']) && $mobileNumberHF['IS_VALID'] != 0? How can it be not set and have a value != 0? Commented Apr 1, 2016 at 21:38

3 Answers 3

1

It doesn't seem to be a correct logic.
I suppose, I should be as follows:

if (isset($mobileNumberHF['IS_VALID'])) {
  echo ($mobileNumberHF['IS_VALID'] != 0)? '<strong class="text-success">Validado</strong>' : '<strong class="text-danger">Inválido</strong>';
} else {
  echo '<strong class="text-warning">Sin validar</strong>';  
}

As for your ternary operator: you have misplaced parenthesis in the last operand. Also 'Hasn't word has unescaped single quote.

$isValidMobileNum = 1;
$mobileNumStatusLabel = ($isValidMobileNum == 1) ? 'Valid' : (!isset($isValidMobileNum) ? 'Hasn\'t been validated' : 'Invalid');
echo $mobileNumStatusLabel; // "Valid"
Sign up to request clarification or add additional context in comments.

2 Comments

You supposed right. This ternary reference worked. Just one question. Why is $mobileNumStatusLabel = $isValidMobileNum ? 'Valid'... not evaluated as true?
$isValidMobileNum ? as condition expression is always eveluated as boolean value. NULL , "" and 0 will be evaluated as FALSE, and 1 - as TRUE
0

Put parentheses around your nested ternary statements to make the output more reliable:

$isValidMobileNum = $mobileNumHF['IS_VALID'];
$mobileNumStatusLabel = ($isValidMobileNum == 1) ? 'Valid' : ( (!isset($isValidMobileNum)) ? 'Hasn't been validated' : 'Invalid' );

Here is an alternative that I would use personally (with little justification for why it's better)

$mobilePhone = isset($mobileNumHF['IS_VALID']) ? $mobileNumHF['IS_VALID'] : -1;

$mobileNumStatusLabel = $mobilePhone ? 'Valid' : ($mobilePhone == 0 ? 'Invalid' : 'Hasn\'t been validated');

This way will avoid an illegal offset warning.

Comments

0

Why do you want to use shorthand syntax for this logic? I mean... obviously you're already confused about what your if else statement is doing. How is it simplifying? Nesting shorthand if/else statements... is this called simplifying these days? To me it just looks like you're creating a monster because you want to write less lines of code. Thats not simplifying, its juggling.

But yes... If you want to make it work, you should put parenthesis around your nested if/else.

I would seriously consider using normal if/else though... If you don't, your code will become impossible to maintain... What if you need to add more logic to your if/else later? What if someone stumbles upon this line of code when tracking down a bug?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.