By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,510 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,510 IT Pros & Developers. It's quick & easy.

'0' passed to function treated as 'null'

P: n/a
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 <inputis
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 <input>.

Simplified example:

function input($value=null) {

$input = '<input';
if ($value)
$input .= ' value="' . $value . '"';
else
if ($type == 'checkbox' || $type == 'radio')
echo('you must specify a value for the attribute "value" of
<input>');
$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
Dec 28 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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_*************************@hotmail.com
Jabber:iv*********@jabber.org ; iv*********@kdetalk.net
Dec 28 '07 #2

P: n/a
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 <inputis
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 <input>.

Simplified example:

function input($value=null) {

$input = '<input';
if ($value)
$input .= ' value="' . $value . '"';
else
if ($type == 'checkbox' || $type == 'radio')
echo('you must specify a value for the attribute "value" of
<input>');
$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
an*****@mypetprogrammer.com

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

P: n/a
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.
Dec 28 '07 #4

P: n/a
(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?)
Dec 28 '07 #5

P: n/a
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
an*****@mypetprogrammer.com

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

P: n/a
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
Dec 28 '07 #7

P: n/a
..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
Dec 28 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.