[man]strlen[/man] or [man]count[/man]. Use strlen for strings and count for everything else.

    Is there a function to test which data type a piece of code is?

      Originally posted by Nibinaear
      Is there a function to test which data type a piece of code is?

      gettype()

      its faster to just test for the type your expecting though, for example

      if (is_string($foo)) {

      }

      theres a specific is_****() function for every data type

        By the way, I use count() for strings, too. It's just a matter of which one you prefer.

          I use count() for strings

          Do you mean instead of strlen()? How do you do that?

            I got curious of the post where somebody said that he uses count() for strings as well. So I've done a little test.

            Using count()

            <?php
            $string = "Cool";
            $length = count($string);
            echo "$length";
            ?>

            This returns 1

            Using strlen()

            <?php
            $string = "Cool";
            $length = strlen($string);
            echo "$length";
            ?>

            This returns 4

            Therefore it is obvious that even if you DO use count() you get incorrect results. So, as somebody said, use strlen() for strings.

              You could have just read the PHP manual, Lars, that would have told you that if the argument to count() is a scalar that is not empty, it would return 1.

                Originally posted by rehfeld
                gettype()

                its faster to just test for the type your expecting though, for example

                if (is_string($foo)) {

                }

                theres a specific is_****() function for every data type

                Yea, I can see why that might be, if you type:

                $type = gettype($number);
                
                if($type == "string")
                {
                  echo $number;
                } 
                

                You're just doing the same thing, it sort of limits the use of that function.

                  Yep. gettype() is most usefull when used in conjunction with a switch construct on the type returned. I use it for client data because they mostly send it in spreadsheet and it end up being columns in any order and after Excel has bashed it about a bit.

                    Originally posted by Installer
                    Do you mean instead of strlen()? How do you do that?

                    You're right, I've got no idea what I was on about. Must've been too late at night. 😕

                    [Edit: I was thinking of JavaScript. In JavaScript, Array.length and String.length both work. Anyway, sorry for the confusion...]

                      Originally posted by Roger Ramjet
                      Yep. gettype() is most usefull when used in conjunction with a switch construct on the type returned. I use it for client data because they mostly send it in spreadsheet and it end up being columns in any order and after Excel has bashed it about a bit.

                      Although it's best to note the warning given on the gettype() manual page.

                        Write a Reply...