Jump to content

Help with function true false null


cunoodle2

Recommended Posts

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

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?

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?

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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.