detecting difference between 0 and null

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

detecting difference between 0 and null

Post by Benjamin »

Hi there, I wrote this code for a drop down menu to select a minute. When it creates the drop down menu, 0 is selected even though the $minute variable is null. I tried changing the comparison operator to === but that didn't work either. I know this is probably simple but I forgot how to do it.

Code: Select all

if (($minute == '') and ($minute == null)) { echo '<option selected value=""></option>' . "\n"; }
for ($i = 0; $i <= 60; $i++) {
  if ($minute == $i) { echo ' <option selected value="' . $i . '">' . $i . '</option>' . "\n";
  } else {
  echo '<option value="' . $i . '">' . $i . '</option>' . "\n";
  }
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

if ($minute === null) { echo '<option selected value=""></option>' . "\n"; }
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Thank feyd I was close, I was using the '' instead of null.
Post Reply