"0" passed to function treated as "null"

"0" passed to function treated as "null"

am 28.12.2007 18:28:02 von rynato

I have a lengthy 'markup.php' file which consists of functions which
take as input the various attribute values of HTML tags and spit out
the HTML tag. My problem is that the function for is
interpreting '0' as null and because I've written the functions so
that they don't write out the attributes which have null value, it
doesn't generate a 'value="0"' attribute for .

Simplified example:

function input($value=null) {

$input = ' if ($value)
$input .= ' value="' . $value . '"';
else
if ($type == 'checkbox' || $type == 'radio')
echo('you must specify a value for the attribute "value" of
');
$input .= ' />';
return $input;
}

So... my question is, how - when passing the argument '0' for $value -
do I make PHP understand that 0 is not null, and so it should go ahead
and write 'value="0"' instead of throwing my error msg?

thx in adv

Re: "0" passed to function treated as "null"

am 28.12.2007 18:32:17 von ivansanchez-alg

rynato wrote:

> So... my question is, how - when passing the argument '0' for $value -
> do I make PHP understand that 0 is not null, and so it should go ahead
> and write 'value="0"' instead of throwing my error msg?

RTFM about type juggling and the === operator. That should clear things up.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

MSN:i_eat_s_p_a_m_for_breakfast@hotmail.com
Jabber:ivansanchez@jabber.org ; ivansanchez@kdetalk.net

Re: "0" passed to function treated as "null"

am 28.12.2007 18:32:39 von My Pet Programmer

rynato said:
> I have a lengthy 'markup.php' file which consists of functions which
> take as input the various attribute values of HTML tags and spit out
> the HTML tag. My problem is that the function for is
> interpreting '0' as null and because I've written the functions so
> that they don't write out the attributes which have null value, it
> doesn't generate a 'value="0"' attribute for .
>
> Simplified example:
>
> function input($value=null) {
>
> $input = ' > if ($value)
> $input .= ' value="' . $value . '"';
> else
> if ($type == 'checkbox' || $type == 'radio')
> echo('you must specify a value for the attribute "value" of
> ');
> $input .= ' />';
> return $input;
> }
>
> So... my question is, how - when passing the argument '0' for $value -
> do I make PHP understand that 0 is not null, and so it should go ahead
> and write 'value="0"' instead of throwing my error msg?
>
> thx in adv
if ($value || $value == 0)

~A!

--
Anthony Levensalor
anthony@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein

Re: "0" passed to function treated as "null"

am 28.12.2007 18:33:27 von rynato

uh, nevermind. I figured it out. For posterity's sake here's the
solution:

instead of:

if ($value)

I changed that conditional to:

if ($value != null || $value === 0)

the value was passed into the function correctly (it still equalled 0)
but for some reason 'if ($value)' was not sufficient for PHP to
distinguish between a value of 0 and no value at all. Can someone
explain this distinction to me? Thanks.

Re: "0" passed to function treated as "null"

am 28.12.2007 18:34:55 von rynato

(that is to say, *I* understand the difference between 0 and null. Why
was ($value) insufficient for PHP to distinguinsh between a value of 0
and no value for $value?)

Re: "0" passed to function treated as "null"

am 28.12.2007 18:35:41 von My Pet Programmer

rynato said:
> uh, nevermind. I figured it out. For posterity's sake here's the
> solution:
>
> instead of:
>
> if ($value)
>
> I changed that conditional to:
>
> if ($value != null || $value === 0)
>
> the value was passed into the function correctly (it still equalled 0)
> but for some reason 'if ($value)' was not sufficient for PHP to
> distinguish between a value of 0 and no value at all. Can someone
> explain this distinction to me? Thanks.
Binary notation: 1 is true, zero is false.

Extrapolated into most programming languages, 0 = false, all other
numbers usually = true when evaluated in boolean expressions

~A!

--
Anthony Levensalor
anthony@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein

Re: "0" passed to function treated as "null"

am 28.12.2007 18:40:08 von ivansanchez-alg

rynato wrote:

> if ($value != null || $value === 0)

Wrong. The correct solution is:

if ($value !== null)

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Trying to make bits uncopyable is like trying to make water not wet.
-- Bruce Schneier

Re: "0" passed to function treated as "null"

am 28.12.2007 18:51:57 von Michael Fesser

..oO(rynato)

>uh, nevermind. I figured it out. For posterity's sake here's the
>solution:
>
>instead of:
>
>if ($value)
>
>I changed that conditional to:
>
>if ($value != null || $value === 0)

if (!is_null($value)) {
...
} else {
...
}

>the value was passed into the function correctly (it still equalled 0)
>but for some reason 'if ($value)' was not sufficient for PHP to
>distinguish between a value of 0 and no value at all. Can someone
>explain this distinction to me? Thanks.

It's explained in the manual (type juggling).

The 'if' statement always expects a boolean expression. If you just pass
a single variable to it like in your case, its type will automatically
be converted to a boolean (also explained in the manual in more detail).
In short: Zero values, empty strings, empty arrays and NULL always
evaluate to FALSE, anything else to TRUE:

0 == 0.0 == '0' == '' == array() == NULL == FALSE

Micha