View Full Version : Null value calulation?

11-22-2006, 09:07 PM
I have a form with 2 text boxes and a button that calls a php calculation and displays the result.
I want to have it so that if it returns a zero or a null it displays nothing

If the user leaves the text boxes blank the result of the php if statment returns the number 0.

If the user inputs 0's into the text boxes it returns nothing as expected

If the user inputs numbers it displays "value = 67.98"

I can't figure out where 0 is comming from as the if statement is not being fulfilled so it shouldn't be displaying anything.

Here is the code for the calculation

$item2tot = floatval ($POST['item2']) * floatval ($POST['qty2']);

Here is the code for the if statement

<?php if ($item2tot != '0'){ echo "value = $item2tot" ;} ?>

11-22-2006, 09:09 PM
get rid of the ticks around the 0 in your if statement... maybe

11-22-2006, 09:17 PM
tried that, still same result

11-22-2006, 09:21 PM
I even tried wraping the calculation in an if text bow != NULL and it oddly still returns the same result. I also tried adding an || != NULL in the display if and still the same

11-22-2006, 10:22 PM
Use double !== instead of !=

11-22-2006, 11:13 PM
PHP can convert from any type to any other (with very few exceptions). To check if something is of some type you can use one of the is_* functions (or the instanceof operator in some cases) or do identity comparison as GeXus suggested.
In PHP all POST and GET data is of type string, so an identity comparison to null will always return false. You could use the empty() (http://www.php.net/empty) construct or strlen() (http://www.php.net/strlen) to check if a string has any content.

11-23-2006, 08:26 PM
I gotcha thanks, worked like a charm. here was the final display code i used.

<?php if (!empty($item2tot)){ echo "$ " . number_format ($item2tot,2);} ?>