WTF? String = Integer in PHP? Strlen() in integers WORKS? HELP!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.php

    WTF? String = Integer in PHP? Strlen() in integers WORKS? HELP!

    I'm involved in a rather nasty debate involving a strange issue
    (whereby the exasperated tell me to RTFM even after my having done so),
    where this is insanely possible:

    [PHP]
    print_r(is_int( '1')); // PRINTS NOTHING
    print_r(strlen( (int)1)); // PRINTS '1'
    [/PHP]

    Now I understand that in PHP, everything scalar is a string and can
    take on the role of an integer, or a boolean or.. whatever it's
    configured to look like. Why is it that I'm so "way off" in this
    thread, when it seems to me that I have it right based on my basic
    understanding the definition of "types"?

    Phil



  • d

    #2
    Re: WTF? String = Integer in PHP? Strlen() in integers WORKS? HELP!

    "comp.lang. php" <phillip.s.powe ll@gmail.com> wrote in message
    news:1138986214 .291779.125980@ g49g2000cwa.goo glegroups.com.. .[color=blue]
    > I'm involved in a rather nasty debate involving a strange issue
    > (whereby the exasperated tell me to RTFM even after my having done so),
    > where this is insanely possible:
    >
    > [PHP]
    > print_r(is_int( '1')); // PRINTS NOTHING
    > print_r(strlen( (int)1)); // PRINTS '1'
    > [/PHP]
    >
    > Now I understand that in PHP, everything scalar is a string and can
    > take on the role of an integer, or a boolean or.. whatever it's
    > configured to look like. Why is it that I'm so "way off" in this
    > thread, when it seems to me that I have it right based on my basic
    > understanding the definition of "types"?
    >
    > Phil
    >
    > http://phpbuilder.com/board/showthread.php?t=10316949
    >[/color]

    If you try that with var_dump, you get a false and a 1, which is what you'd
    expect... Am I missing something here?? :)


    Comment

    • Justin Koivisto

      #3
      Re: WTF? String = Integer in PHP? Strlen() in integers WORKS? HELP!

      comp.lang.php wrote:[color=blue]
      > I'm involved in a rather nasty debate involving a strange issue
      > (whereby the exasperated tell me to RTFM even after my having done so),
      > where this is insanely possible:
      >
      > [PHP]
      > print_r(is_int( '1')); // PRINTS NOTHING
      > print_r(strlen( (int)1)); // PRINTS '1'
      > [/PHP]
      >
      > Now I understand that in PHP, everything scalar is a string and can
      > take on the role of an integer, or a boolean or.. whatever it's
      > configured to look like. Why is it that I'm so "way off" in this
      > thread, when it seems to me that I have it right based on my basic
      > understanding the definition of "types"?
      >
      > Phil
      >
      > http://phpbuilder.com/board/showthread.php?t=10316949[/color]

      strlen() expects a string as a parameter. because of the "typeless"
      nature of PHP, when something other than a string is sent, PHP will
      automatically "cast" it to a string (like using the strval() function).
      Therefore, when the length is calculated, it is operating on the
      equivalent to $int = strval($int) instead of the actual integer.

      The same type of thing happens when using expressions:

      ('1' == TRUE) will result in a bool(true). This is because the lowest
      common type between string and bool is bool (the simplest of all).
      Therefore, the string is cast to a boolean val, then the comparison is made.

      Consider var_dump('my string'==1). This returns bool(false) because the
      string is cast to an integer (the simplest common type) which is 0.

      In constrast, var_dump('1 string'==1) return bool(true) because the
      intval('1 string') === 1


      The type Juggling section of the manual is about the best reference you
      can use for this kind of thing, but IMO it isn't nearly as complete as
      it could be for a novice level explanation.



      --
      Justin Koivisto, ZCE - justin@koivi.co m

      Comment

      • Wayne

        #4
        Re: WTF? String = Integer in PHP? Strlen() in integers WORKS? HELP!

        On 3 Feb 2006 09:03:34 -0800, "comp.lang. php"
        <phillip.s.powe ll@gmail.com> wrote:
        [color=blue]
        >I'm involved in a rather nasty debate involving a strange issue
        >(whereby the exasperated tell me to RTFM even after my having done so),
        >where this is insanely possible:
        >
        >[PHP]
        >print_r(is_int ('1')); // PRINTS NOTHING
        >print_r(strlen ((int)1)); // PRINTS '1'
        >[/PHP]
        >
        >Now I understand that in PHP, everything scalar is a string[/color]

        *bzzt* wrong. This is where you are getting lost.
        [color=blue]
        >and can take on the role of an integer, or a boolean or..[/color]

        Everything has a type -- so integers are integers and strings are
        strings and booleans are booleans. However, PHP will automatically
        cast scalars to different types as appropriate for a function or
        operator.

        So in this case:

        var_dump(is_int ('1')) // prints false, it's a string
        var_dump(is_int (1)) // prints true, is an int
        var_dump(is_str ing('1')) // prints true, is a string
        var_dump(is_str ing(false)) // prints false, is a boolean
        echo strlen(144) // prints 3 because it casts the number to a string

        The is_* functions return the type of the variable -- they do not care
        about the contents themselves. So it doesn't care that it's a string
        containing an integer.

        You can use the is_numeric() function to see if a string value
        contains a number.

        PHP will automatically cast in cases where you do something like this:

        $x = '12' + '16'; // $x will contain 28.

        And you can manually cast values yourself:

        $x = (integer)'12'; // $x will contain an integer of 12
        $x = (string)8l // $x will contain the string '8'
        [color=blue]
        >when it seems to me that I have it right based on my basic
        >understandin g the definition of "types"?[/color]

        As I said, you're basic definition is wrong. Not all scalar values in
        PHP are strings.

        Comment

        • Justin Koivisto

          #5
          Re: WTF? String = Integer in PHP? Strlen() in integers WORKS? HELP!

          Wayne wrote:[color=blue]
          > On 3 Feb 2006 09:03:34 -0800, "comp.lang. php"
          > <phillip.s.powe ll@gmail.com> wrote:
          >[color=green]
          >> I'm involved in a rather nasty debate involving a strange issue
          >> (whereby the exasperated tell me to RTFM even after my having done so),
          >> where this is insanely possible:
          >>
          >> [PHP]
          >> print_r(is_int( '1')); // PRINTS NOTHING
          >> print_r(strlen( (int)1)); // PRINTS '1'
          >> [/PHP]
          >>
          >> Now I understand that in PHP, everything scalar is a string[/color]
          >
          > *bzzt* wrong. This is where you are getting lost.
          >[color=green]
          >> and can take on the role of an integer, or a boolean or..[/color]
          >
          > Everything has a type -- so integers are integers and strings are
          > strings and booleans are booleans. However, PHP will automatically
          > cast scalars to different types as appropriate for a function or
          > operator.[/color]

          That phrasing is a little confusing...
          integer, float, string and boolean are *all* scalar types. (The only 4
          scalar types supported by PHP.)

          Supported types that are not scalar are:
          * the 2 compound types array and object
          * the 2 special types NULL and resource.

          PHP will automatically cast to the lowest common type in expressions.
          Therefore comparing an array to an object will cause both to be cast to
          a string first.
          [color=blue]
          > So in this case:
          >
          > var_dump(is_int ('1')) // prints false, it's a string
          > var_dump(is_int (1)) // prints true, is an int
          > var_dump(is_str ing('1')) // prints true, is a string
          > var_dump(is_str ing(false)) // prints false, is a boolean
          > echo strlen(144) // prints 3 because it casts the number to a string
          >
          > The is_* functions return the type of the variable -- they do not care
          > about the contents themselves. So it doesn't care that it's a string
          > containing an integer.
          >
          > You can use the is_numeric() function to see if a string value
          > contains a number.[/color]

          To test for an integer from a string, use something like:

          if(is_numeric(s tring) && intval($string) ==$string){
          // $string contains a string representation of an integer
          }else{
          // not an integer (it's a float)
          }
          [color=blue]
          > PHP will automatically cast in cases where you do something like this:
          >
          > $x = '12' + '16'; // $x will contain 28.
          >
          > And you can manually cast values yourself:
          >
          > $x = (integer)'12'; // $x will contain an integer of 12
          > $x = (string)8l // $x will contain the string '8'
          >[color=green]
          >> when it seems to me that I have it right based on my basic
          >> understanding the definition of "types"?[/color]
          >
          > As I said, you're basic definition is wrong. Not all scalar values in
          > PHP are strings.[/color]

          ....but all integer, float, string and boolean values are scalar. ;)

          --
          Justin Koivisto, ZCE - justin@koivi.co m

          Comment

          • Colin McKinnon

            #6
            Re: WTF? String = Integer in PHP? Strlen() in integers WORKS? HELP!

            Justin Koivisto wrote:
            [color=blue]
            > Wayne wrote:[color=green]
            >> On 3 Feb 2006 09:03:34 -0800, "comp.lang. php"
            >> <phillip.s.powe ll@gmail.com> wrote:
            >>[color=darkred]
            >>> [PHP]
            >>> print_r(is_int( '1')); // PRINTS NOTHING
            >>> print_r(strlen( (int)1)); // PRINTS '1'
            >>> [/PHP]
            >>>
            >>> Now I understand that in PHP, everything scalar is a string[/color]
            >>
            >> *bzzt* wrong. This is where you are getting lost.
            >>
            >> Everything has a type -- so integers are integers and strings are
            >> strings and booleans are booleans. However, PHP will automatically
            >> cast scalars to different types as appropriate for a function or
            >> operator.[/color]
            >
            > That phrasing is a little confusing...
            > integer, float, string and boolean are *all* scalar types. (The only 4
            > scalar types supported by PHP.)
            >
            > Supported types that are not scalar are:
            > * the 2 compound types array and object
            > * the 2 special types NULL and resource.
            >[/color]

            *bzzt* wrong

            PHP references are scalar - but in most instances dereferencing is
            transparent and higher precedence than casting.

            I believe that resources and NULL are also scalars but not castable.

            C.

            Comment

            • Wayne

              #7
              Re: WTF? String = Integer in PHP? Strlen() in integers WORKS? HELP!

              On Sun, 05 Feb 2006 21:54:03 GMT, Colin McKinnon
              <colin.thisisno tmysurname@ntlw orld.deletemeun lessURaBot.com> wrote:
              [color=blue][color=green]
              >> Supported types that are not scalar are:
              >> * the 2 compound types array and object
              >> * the 2 special types NULL and resource.
              >>[/color]
              >
              >*bzzt* wrong
              >
              >PHP references are scalar - but in most instances dereferencing is
              >transparent and higher precedence than casting.[/color]

              References don't really factor into this discussion.
              [color=blue]
              >I believe that resources and NULL are also scalars but not castable.[/color]

              Maybe you should test your beliefs...

              if (!is_scalar(nul l)) echo 'I believe you are wrong!';

              I was a bit surprised that null was not a scalar -- seems logical that
              it would be, but I guess the designers of PHP were trying to lump them
              in with objects.

              Comment

              Working...