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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts

    IF statment not working correctly

    This is probably a simple issue but i cant seem to figure out why.

    PHP Code:
    if (count($_POST) !== "10")
        {
            echo 
    "An error occured (001)<br />";
            echo 
    count($_POST);
        } 
    That always echoes An error occured etc, but the count($_POST) echoes 10.

    Anyone see my problem?

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You are doing a strict type comparison of an integer against a string. It will never be true. You should use one of these instead:
    PHP Code:
    if (count($_POST) !== 10

        echo 
    "An error occured (001)<br />"
        echo 
    count($_POST); 
    }

    // or

    if (count($_POST) != 10// you can use a string for 10, but it doesn't make sense if you are specifying it in code 

        echo 
    "An error occured (001)<br />"
        echo 
    count($_POST); 


  • #3
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    It isn't working because you are comparing an integer to a string and you using the !== comparison operator which means "a and b are not equal or not the same type".

    Change it to:
    PHP Code:
    if (count($_POST) !== 10
    Is there a reason you used '!==' instead of '!=' in your if statement? In any event you shouldn't write the number as a string since PHP would have to do a type conversion.
    OracleGuy

  • #4
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Theres no reason i used !==. So your saying it would be better using this?

    PHP Code:
    if (count($_POST) !== "10"

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by tomharto View Post
    Theres no reason i used !==. So your saying it would be better using this?

    PHP Code:
    if (count($_POST) !== "10"
    No... it would be better using this:
    PHP Code:
    if (count($_POST) !== 10

  • #6
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Okay, thanks


  •  

    Posting Permissions

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