I have a function in my website where you can buy stuff. After you do that, you will get an email. Everything works fine except for one thing. I have an ifstatement in my code, but it always takes the if and not the else if or else path.

I searched on the Internet but I couldn't find any solution. I tried different things in my else if like else if (isset empty($registrationInfo['serviceselectoptie']) and else if (isset ($registrationInfo['serviceselectoptie'] === NULL) but that didn't work.

My if statement

<?php
  $registrationInfo = $connect->select(
    "SELECT * FROM `registrationinfo` WHERE `orderId`= '".$order_id."' LIMIT 1"
  )->fetch();

  if (
    isset($registrationInfo['serviceselectoptie']) AND 
    isset($registrationInfo['serviceconsult'])
  ) { 
    $titellist = '<tr><td bgcolor="#DEDEDE" style="padding: 5px 5px 5px 5px;width:175px;">' 
      .$registrationInfo['serviceselectoptie'].':</td><td style="padding: 5px 5px 5px 5px;">'
      .$registrationInfo['serviceconsult'] .'</td></tr>'; 
  } else if (
    !isset($registrationInfo['serviceselectoptie']) AND
    isset($registrationInfo['serviceconsult'])
  ) {
    $titellist = '<tr><td bgcolor="#DEDEDE" style="padding: 5px 5px 5px 5px;width:175px;">'
     .'Bestelling:</td><td style="padding: 5px 5px 5px 5px;">'
     .$registrationInfo['serviceconsult'].'</td></tr>';
  } else { 
    $titellist = "";
  }
?>

What I expect is that if the $registrationInfo['serviceselectoptie'] row is empty (null), it will go to the else if. My actual output is that it takes the if path and because the row is empty it will just give :

Example of output

example output

2 Answers

3
Nigel Ren On Best Solutions

As you are checking the details in a result set, you will find that they will always be set, what you may want instead is to check if the value is empty(), as you want to make sure there is a value though - you will have to say if !empty() to check if it has a value...

if(!empty($registrationInfo['serviceselectoptie']) 
      AND !empty($registrationInfo['serviceconsult'])) 

apply this to the other parts of the code as well.

0
Oluwafemi Sule On

isset($registrationInfo['serviceselectoptie']) is truthy when serviceselectoptie key exists in the array and is set to either an empty string or a value other than NULL.

// php -a
// Interactive shell

> $test = ['a'=>2, 'b'=>null, 'c'=>''];

> echo isset($test['a']);
 1
> echo isset($test['b']);
> echo isset($test['c']);
1

A better check is using empty function.

// php -a
// Interactive shell

> $test = ['a'=>2, 'b'=>null, 'c'=>''];

> echo empty($test['a']);
> echo empty($test['b']);
1
> echo empty($test['c']);
1
> echo empty($test['d']); // non-existent key 
1
> echo empty(NULL['a']); // yes NULL can be indexed
1

This way the branches in your code will be as:


if(
    !empty($registrationInfo['serviceselectoptie'])
    AND !empty($registrationInfo['serviceconsult'])) { 
    //... 

} 
else if(
    empty($registrationInfo['serviceselectoptie'])
    AND !empty($registrationInfo['serviceconsult'])) {
    //...
} 
else { 
    //... 
}