'0' passed to function treated as 'null'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rynato

    '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 <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=nu ll) {

    $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
  • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

    #2
    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_breakfas t@hotmail.com
    Jabber:ivansanc hez@jabber.org ; ivansanchez@kde talk.net

    Comment

    • My Pet Programmer

      #3
      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 <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=nu ll) {
      >
      $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@mypetpr ogrammer.com

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

      Comment

      • rynato

        #4
        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.

        Comment

        • rynato

          #5
          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?)

          Comment

          • My Pet Programmer

            #6
            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@mypetpr ogrammer.com

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

            Comment

            • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

              #7
              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

              Comment

              • Michael Fesser

                #8
                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($valu e)) {
                ...
                } 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

                Comment

                Working...