cunoodle2 Posted July 8, 2009 Share Posted July 8, 2009 I'm trying to just put together a basic function here but the problem is that I'm having a hard time deciphering between "Null" and 0. Here is this oh so basic function... <?php function PrintOptions($name,$selected) { if ($selected != NULL) $selected = intval($selected); echo "<input type=\"radio\" value=\"1\" name=\"$name\" "; echo (1 == $selected)?"checked>Yes\n":">Yes\n"; echo "<input type=\"radio\" value=\"0\" name=\"$name\" "; echo (0 == $selected)?"checked>No\n":">No\n"; } ?> I ran test data with this... <?php PrintOptions("testYes",1); PrintOptions("testNo",0); PrintOptions("testNull",NULL); ?> Here is the html code produced.. <input type="radio" value="1" name="testYes" checked>Yes <input type="radio" value="0" name="testYes" >No <input type="radio" value="1" name="testNo" >Yes <input type="radio" value="0" name="testNo" checked>No <input type="radio" value="1" name="testNull" >Yes <input type="radio" value="0" name="testNull" checked>No I'm basically just trying to have it check the radio option ONLY when a selection is made. If no selection (NULL) then don't check anything at all. I've gone as far in the function to even temporarily assign a different value to "selected" if it was Null but I can't seem to properly differentiate between NULL and 0. Any input? Link to comment https://forums.phpfreaks.com/topic/165278-help-with-function-true-false-null/ Share on other sites More sharing options...
gevans Posted July 9, 2009 Share Posted July 9, 2009 How about this... if (!is_null($selected)) Link to comment https://forums.phpfreaks.com/topic/165278-help-with-function-true-false-null/#findComment-871602 Share on other sites More sharing options...
cunoodle2 Posted July 9, 2009 Author Share Posted July 9, 2009 How about this... if (!is_null($selected)) That seems to work a little but I'm still getting that "no" box checked. Here is my source example... <?php function PrintOptions($name,$selected) { if (!is_null($selected)) $selected = intval($selected); echo "<input type=\"radio\" value=\"1\" name=\"$name\" "; echo (1 == $selected)?"checked>Yes\n":">Yes\n"; echo "<input type=\"radio\" value=\"0\" name=\"$name\" "; echo (0 == $selected)?"checked>No\n":">No\n"; } ?> Test data... <?php PrintOptions("testNull",NULL); ?> Html code produced.. <input type="radio" value="1" name="testNull" >Yes <input type="radio" value="0" name="testNull" checked>No Why is it still checking the "no"?? '$selected' is not equal to zero. It's NULL. Any ideas here? Link to comment https://forums.phpfreaks.com/topic/165278-help-with-function-true-false-null/#findComment-871665 Share on other sites More sharing options...
cunoodle2 Posted July 10, 2009 Author Share Posted July 10, 2009 Anyone else have input into why NULL and 0 (<-- that is the number zero) are being treated as one in the same? Link to comment https://forums.phpfreaks.com/topic/165278-help-with-function-true-false-null/#findComment-872413 Share on other sites More sharing options...
corbin Posted July 10, 2009 Share Posted July 10, 2009 Because intval(0) and intval(null) both return 0..... Link to comment https://forums.phpfreaks.com/topic/165278-help-with-function-true-false-null/#findComment-872414 Share on other sites More sharing options...
cunoodle2 Posted July 10, 2009 Author Share Posted July 10, 2009 Because intval(0) and intval(null) both return 0..... Yes you are correct. That is why I have this line in my function... " if (!is_null($selected)) $selected = intval($selected);" So basically if the item ISN'T null THEN take the intval(). Otherwise it should just be leaving it assigned as NULL right? Link to comment https://forums.phpfreaks.com/topic/165278-help-with-function-true-false-null/#findComment-872417 Share on other sites More sharing options...
corbin Posted July 10, 2009 Share Posted July 10, 2009 Oh, yeah, sorry.... I'm blind today . Didn't see that. I just learned something new about PHP today.... F:\Users\Corbin>php -r "echo var_dump(null == 0);" bool(true) Apparently PHP thinks null is equal to 0 x.x. Weird. Anyway, one option would be to do: if($selected !== null && $selected == 0) Or, you could do: if($selected === 0) Link to comment https://forums.phpfreaks.com/topic/165278-help-with-function-true-false-null/#findComment-872418 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.