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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
..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 |
![]() |
| Thread Tools | |
| Display Modes | |
|
|