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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    187
    Thanks
    6
    Thanked 0 Times in 0 Posts

    PHP empty and 0 values

    How do I get php to check if a value is empty but not if the value is "0"

    I have a table that users input data into by month. For example there might be these rows for january.

    A. Price
    B. Savings
    C. Total
    D. MESSAGE (ie. Savings/No Savings)

    A & B are users input and C / D are results based on the input

    USER INPUT
    A. $100
    B. $20
    RESULTS ROWS
    C. $80
    D. SAVINGS

    So if a user doesn't enter data into A & B the result of C = nothing and no message is generated in D. But the problem is that if someone inupts something like A = $100 B = $100... the result of C becomes "0" which is considered an empty() value... so Row C doesn't show up.

    So how can I get it to check if a row is "empty" but not "0"
    Last edited by a4udi; 01-31-2012 at 03:22 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    PHP Code:
    $var 0;
    if (empty(
    $var) && $var !== 0)
    {
        
    // value is bad
    }
    else
    {
        print 
    'value is available or 0';

    && requires all parameters to be evaluated to true. empty is true for both '0' and 0, but throw a check in to see if its not 0. So with the above, 0 will pass the empty check, but fail the $var != 0 check.
    You can flip the check if you don't care about the empty. Invert both of the comparisons and use an || instead of an &&.

    Datatype does matter here, so you need to use identical comparisons especially if you invert. Since "" == 0 == '0' == false == array(), it would otherwise be impossible to tell whether the value is empty when comparing to a 0.

  • #3
    Regular Coder
    Join Date
    Jan 2005
    Posts
    187
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I figured out how to get that operator in there but not sure if it's working correctly. If I input "0" as one of the values in row A or B, it does not show anything in the savings row still.

    Example of inputs for the code below:
    A. 100.00
    B. 0.00
    C: (no value shows up)

    shouldn't it check if "B" is empty and it's not "0" then it prints nothing... but if it is "0.00" it should show $savings[0] ?

    PHP Code:
    <?php $savings[0] = $item['janinput1'] - $item['janinput2']; ?>
    <?php 
    if (empty($item['janinput1']) && $item['janinput1'] !== 0) {
    echo 
    ' '
    } elseif (empty(
    $item['janinput2']) && $item['janinput2'] !== 0) {
    echo 
    ' '
    } else {
    echo 
    number_format($loanserviceratio[0], 2) . '%'; } ?>
    And I tried the code you gave me above with the extra brackets but it gives me " syntax error, unexpected T_BOOLEAN_AND" with that.

    PHP Code:
    if (empty($item['janinput1'])) && $item['janinput1'] !== 0
    Last edited by a4udi; 01-30-2012 at 03:46 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Its a syntax error, you have too many closing ) in the first check putting && outside of the if branch.
    These values, it is literally $0.00, or are you formatting that after the fact and it is 0 / 0.0. Unfortunately, like I said the datatype does matter. You can cast comparisons so a float could be cast to a double or vice verse to compare 0.0 to the value or 0.

    So long as $item['janinput1'] and $item['janinput2'] exist even if they are empty, $savings[0] will become 0 (cast to an integer). Since if they don't exist they will become '', and '' - '' = 0 (since operations only work on numerical datatypes, '' = 0, so '' - '' = 0 - 0 which is 0). You'll only get an error if these offsets don't exist at all, but the end result will still be 0 (as null - null = 0).

    The better option with what you are describing above is simply to handle the $savings instead. No matter what happens with any other value, $savings will be a number so you can simply check if its 0 or not, and since you can use loose comparison it won't matter if its a float or double. Unless I misunderstand what you are printing out here.

  • Users who have thanked Fou-Lu for this post:

    a4udi (01-31-2012)

  • #5
    Regular Coder
    Join Date
    Jan 2005
    Posts
    187
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks for your help! Sorry, it was a lot of extra code in there as well but I couldn't really past the 1000s of lines but you were right, it had to do with the variable types and ended up making an extra "check" variable to compare with and solve the issue.


  •  

    Posting Permissions

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