View Full Version : Resolved PHP empty and 0 values

01-30-2012, 03:51 PM
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

A. $100
B. $20
C. $80

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"

01-30-2012, 03:57 PM
$var = 0;
if (empty($var) && $var !== 0)
// value is bad
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.

01-30-2012, 04:42 PM
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 $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.

if (empty($item['janinput1'])) && $item['janinput1'] !== 0)

01-30-2012, 04:44 PM
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.

01-31-2012, 04:20 PM
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.