'0' passed to function treated as 'null'

This is a discussion on '0' passed to function treated as 'null' within the PHP Language forums, part of the PHP Programming Forums category; I have a lengthy 'markup.php' file which consists of functions which take as input the various attribute values of ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-28-2007
rynato
 
Posts: n/a
Default '0' passed to function treated as 'null'

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 <input> 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 <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
Reply With Quote
  #2 (permalink)  
Old 12-28-2007
Iván Sánchez Ortega
 
Posts: n/a
Default Re: '0' passed to function treated as 'null'

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
Reply With Quote
  #3 (permalink)  
Old 12-28-2007
My Pet Programmer
 
Posts: n/a
Default Re: '0' passed to function treated as 'null'

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 <input> 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 <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
anthony@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Reply With Quote
  #4 (permalink)  
Old 12-28-2007
rynato
 
Posts: n/a
Default Re: '0' passed to function treated as 'null'

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.
Reply With Quote
  #5 (permalink)  
Old 12-28-2007
rynato
 
Posts: n/a
Default Re: '0' passed to function treated as 'null'

(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?)
Reply With Quote
  #6 (permalink)  
Old 12-28-2007
My Pet Programmer
 
Posts: n/a
Default Re: '0' passed to function treated as 'null'

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
Reply With Quote
  #7 (permalink)  
Old 12-28-2007
Iván Sánchez Ortega
 
Posts: n/a
Default Re: '0' passed to function treated as 'null'

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
Reply With Quote
  #8 (permalink)  
Old 12-28-2007
Michael Fesser
 
Posts: n/a
Default Re: '0' passed to function treated as 'null'

..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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:41 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0