-6

how i can turn in PHP the $validation output from 1/0 to the word "valid/invalid"? Do i need json_encode($validation) in this case?

    $json = json_decode($content, true);

    if ($json['error'] == NULL ) {
        $country = $json['result']['countryCode'];
        $vatNumber = $json['result']['vatNumber'];
        $validation = $json['result']['valid'];               

          echo "
          <dt>Valid:</dt>
          <dd>$validation</dd>
          <dt>VAT-Number:</dt>
          <dd>$country$vatNumber</dd>

        } else {
    echo "error";
    }

Thank you!

CrandellWS
  • 2,708
  • 5
  • 49
  • 111
  • 3
    Use if-else statement or ternary operator – sectus May 26 '14 at 08:11
  • @sectus had a point in comments for @agro answer...I updated my answer to reflect a (secondary) validation `$validation === 1` in attempt to avoid non-desired valid returns. – CrandellWS May 26 '14 at 09:36

3 Answers3

0

how about this:

...
<dd>" . ($validation ? 'valid' : 'invalid') . "</dd>
...

or with a secondary check

...
<dd>" . ($validation == 1 ? 'valid' : 'invalid') . "</dd>
...
agro
  • 206
  • 1
  • 2
  • 7
  • better to be short and sweet as shown in this answer, which will get the same results as I suggested. – CrandellWS May 26 '14 at 08:15
  • What about precedence? – sectus May 26 '14 at 08:16
  • @sectus notice [example 4](http://php.net/language.operators.comparison#language.operators.comparison.ternary) as [mentioned](http://stackoverflow.com/a/6224443/1815624) is Non-obvious Ternary Behaviour and [user3675536](http://stackoverflow.com/users/3675536/user3675536) is obviously a beginner PHP programmer... – CrandellWS May 26 '14 at 08:53
  • @CrandellWS , reread answer. Try to reproduce code of the answer. Then think about operator precedence (concatenation and ternary). – sectus May 26 '14 at 09:03
  • @sectus I am not understanding as `$validation ? 'valid' : 'invalid'` is the same as `(($validation ? true : false) ? 'valid' : 'invalid'` only a problem if it wasn't a `1||0` `true || false` value... – CrandellWS May 26 '14 at 09:23
  • @sectus A consideration from a security point `((($validation === 1) ? true : false) ? 'valid' : 'invalid')` – CrandellWS May 26 '14 at 09:28
  • @CrandellWS , I am talking about operator precedence. Precedence of concationation is higher than precedence of ternary. So `echo "
    " . $validation ? 'valid' : 'invalid' . "
    ";` would always output 'valid' instead of '
    valid
    ' or of '
    invalid
    '. You need to add parentheses to change precedence: `echo "
    " . ($validation ? 'valid' : 'invalid') . "
    ";`
    – sectus May 27 '14 at 01:39
  • he is right, so my first answer was wrong because of the precedence. `"
    " . $validation ? 'valid' : 'invalid' . "
    "` will be interpreted as `("
    " . $validation) ? 'valid' : ('invalid' . "
    ")`. but my second one will work fine.
    – agro May 27 '14 at 07:43
0

simple as 1 == true and 0 == false

if($validation === 1){
    $validation = 'valid';
} else {
    $validation = 'invalid';
}

have a reading of the php docs on boolean

CrandellWS
  • 2,708
  • 5
  • 49
  • 111
0

You could use an array:-

$validOrNot = ['invalid', 'valid'];

//some other code 'n stuff
$validation = $validOrNot[$json['result']['valid']];
vascowhite
  • 18,120
  • 9
  • 61
  • 77