Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Gil
    Gil is offline
    New Coder
    Join Date
    Jul 2002
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 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
    New Coder
    Join Date
    Jun 2002
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    Gil
    Gil is offline
    New Coder
    Join Date
    Jul 2002
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That manual section explains it all:
    http://www.php.net/manual/en/languag...oolean.casting

    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.

  • #5
    New Coder
    Join Date
    Jul 2002
    Location
    Regina, Saskatchewan, Canada
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Gil
    Gil is offline
    New Coder
    Join Date
    Jul 2002
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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; 07-12-2002 at 09:09 PM.

  • #7
    New Coder
    Join Date
    Jun 2002
    Location
    Paris, France
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •