Web Analytics Made Easy -
StatCounter IF statment not working correctly - CodingForum

Announcement

Collapse
No announcement yet.

IF statment not working correctly

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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); 

    Comment


    • #3
      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

      Comment


      • #4
        Theres no reason i used !==. So your saying it would be better using this?

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

        Comment


        • #5
          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

          Comment


          • #6
            Okay, thanks

            Comment

            Working...
            X