-2

I would think this variable would be pretty straight forward but it's not working? It does echo the row as need if it's set but if it's "false" or not set it does not print "N/A"? Am i doing something wrong here?

$term = isset($row['term']) ? $row['term'] : 'N/A';
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
gillweb
  • 91
  • 7
  • 1
    `var_dump($term);` what does that show? The question is short on detail and seems to be database related. We don't know where and how that is coming from also. The schema would be required and values. – Funk Forty Niner Sep 27 '18 at 13:59
  • 1
    `isset()` and `empty()` are two different animals here. Again, we don't know what's in your db. So, can you elaborate on what I asked? I myself have voted to close the question being unclear. – Funk Forty Niner Sep 27 '18 at 14:02
  • 1
    What do you mean by "not working"? I'm guessing `$row['term']` is always set, but may be empty or blank. @FunkFortyNiner is on the right track here. – Jay Blanchard Sep 27 '18 at 14:06
  • 1
    meaning if the database was "empty" it would not output "N/A". by changing the var to use !empty as @Mikey suggested, it's working as expected now since the var was "set" BUT was empty it wouldn't output the "N/A". – gillweb Sep 27 '18 at 14:10
  • actually my var_dump should have been `var_dump($row['term']);`. – Funk Forty Niner Sep 27 '18 at 14:30

1 Answers1

5

My suggestion would be to try the following:

$term = !empty($row['term']) ? $row['term'] : 'N/A';

Potentially you have a scenario where $row['term'] is set but there is no value so it still falls into the true condition condition.

For example the following:

$row = ['term' => ''];

$term = isset($row['term']) ? $row['term'] : 'N/A';

echo $term; // Prints ''
Mikey
  • 2,606
  • 1
  • 12
  • 20
  • Thank you Mikey... i guess the variable was set, even though empty, it wasn't Null. !empty is working perfectly! – gillweb Sep 27 '18 at 14:02
  • 2
    was about to answer same, upvote Mikey's answer. even if your var is true/false it is set so just play with the status of it $term = (!$row['term'] && isset($row['term'])) ? $row['term'] : 'N/A'; Good practice is to var_dump your variables and understand what is happening. – MicPic Sep 27 '18 at 14:04