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
    Regular Coder
    Join Date
    Nov 2006
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Null value calulation?

    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
    Code:
     $item2tot = floatval ($POST['item2']) * floatval ($POST['qty2']);
    Here is the code for the if statement
    Code:
     <?php if ($item2tot != '0'){ echo "value = $item2tot" ;} ?>

  • #2
    New Coder
    Join Date
    Nov 2006
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    get rid of the ticks around the 0 in your if statement... maybe

  • #3
    Regular Coder
    Join Date
    Nov 2006
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    tried that, still same result

  • #4
    Regular Coder
    Join Date
    Nov 2006
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    New Coder
    Join Date
    Nov 2006
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use double !== instead of !=

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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() construct or strlen() to check if a string has any content.
    Last edited by marek_mar; 11-23-2006 at 12:16 AM.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #7
    Regular Coder
    Join Date
    Nov 2006
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I gotcha thanks, worked like a charm. here was the final display code i used.

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


  •  

    Posting Permissions

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