Web Analytics Made Easy -
StatCounter Value=0 is not a value? - CodingForum

Announcement

Collapse
No announcement yet.

Value=0 is not a value?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Value=0 is not a value?

    Hi there,

    <?php

    if ($b03 && $b04 && $b05 && $b07 && $b07 && $b09 && $b10)

    {

    ?>

    display this line

    <?php
    //endif
    }?>

    $b03 has a value of "0" - Why does php treat "0" as no value? No value would be if nothing is entered in this case. 0 is a possible value.

    Can someone explain this to me? Also, if I'm coding things the wrong way here please tell me!

    Thanks,

    Gil

  • #2
    What's the error? I'm not that familiar with the 0 thing, but if its a string, it should be handled correctly. When you set $B03, was it with quotes or just the value. Actually, that shouldn't be the problem but...maybe its treated as a null value since 0 does nothing in PHP. Try making it a string.
    -Obiwan Jabroni
    May the Schwartz be With You

    Comment


    • #3
      Hi There,

      There's no error. It's quite a simple little script with if/else statements.

      $bo3 is a textfield without a default value. I'm checking 5 textfields here for a value and "0" is treated as empty/no value. It doesn't matter if I leave this field empty or enter "0".

      Unfortunately, I'm not too good with strings yet, just my guitar strings.

      Thanks,

      Gil

      Comment


      • #4
        That manual section explains it all:


        So if you have a string "0" and do an implicit cast to boolean (in the if statement), you've got bad luck. Try to use strlen() to determine if a string length is greater than 0 - which indicates that the user has set a value for this input field.
        De gustibus non est disputandum.

        Comment


        • #5
          Try this:

          Do not create a variable for $test (as in, do not give it a value( and enter this:

          if ($test == '0') {
          echo "weird!";
          }

          Notice, it prints weird. 0 is given to any variable without a value. Thus, I guess it got confused!
          Jared Brandt
          IKinsler

          Comment


          • #6
            Hi,
            You are right but the example below shows that "" is not always handled as 0 nor is zero handled as "". I can use this.


            <?php

            $test1 ="0";
            if ($test1=="0"){
            $test1=true;
            }
            if ($test1){
            echo "<p>true";
            }
            else {
            echo "<p>false";
            }

            $test2 ="";
            if ($test2==""){
            $test2=true;
            }
            if ($test2){
            echo "<p>true";
            }
            else {
            echo "<p>false";
            }

            $test3 ="";
            if ($test3=="0"){
            $test3=true;
            }
            if ($test3){
            echo "<p>true";
            }
            else {
            echo "<p>false";
            }

            ?>

            Bye bye

            Gil
            Last edited by Gil; Jul 12, 2002, 03:09 PM.

            Comment


            • #7
              What you can do to see if a variable contains a zero is use the isset(); function :
              PHP Code:
              <?php 

              if (isset($b03) && isset($b04) && isset($b05) && isset($b07) && isset($b07) && isset($b09) && isset($b10)) 



              ?> 

              display this line 

              <?php 
              //endif 
              }?>
              SHould work.....
              I don't suffer from insanity, I enjoy every single minute of it!

              Comment

              Working...
              X