#1
  1. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,187
    Rep Power
    220

    strpos doesnt work with int!


    They should really make this more clear is not valid

    PHP Code:
    $pos strpos($row['package'], $membership_levels_id);
    if(
    $pos === false//do things 
    and this is valid
    PHP Code:
    $pos strpos($row['package'], ''.$membership_levels_id.'');
    if(
    $pos === false//do things 
    strpos does not work with type int. wtf
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,318
    Rep Power
    630
    Think about it. What does 'str' stand for? I don't know how it could be any more clear. As an aside you could also use:
    PHP Code:
    $pos strpos($row['package'],(string)$membership_levels_id);
    $pos strpos($row['package'],strval($membership_levels_id)); 
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,187
    Rep Power
    220
    I think Requinix is 5 years older because of my posts.

    Things like this are proof programming is not for me :0

    But hey, I like it and with my half a$$ knowledge I've been getting by for years
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,318
    Rep Power
    630
    Nah! You are doing fine. We all sometimes can't see the forest for the trees. I think there are many other posters (one in particular comes to mind) that age Requinix, and all of us, much more than you. I'm sure I've added a few months to others myself.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,079
    Rep Power
    4101
    This is called out in the manual:
    needle
    If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
    Why they chose to implement it this way I'm not sure. It doesn't really make sense to me but such is life.

    I suspect it might have something to do with how in C some functions will use int to represent a single character so that behavior was carried forward into PHP.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  10. #6
  11. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,321
    Rep Power
    9645
    I don't age much faster but I do more than I would like.

    Originally Posted by kicken
    Why they chose to implement it this way I'm not sure. It doesn't really make sense to me but such is life.

    I suspect it might have something to do with how in C some functions will use int to represent a single character so that behavior was carried forward into PHP.
    That. C doesn't have a strpos() function but it does have strchr(), which is like PHP's except the "needle" is only one character. Thing is, in C a char and int are basically the same thing, leading to
    Code:
    char* strchr(const char* str, int c)
    so passing either works the same way. Of course looking through a string to find a number is weird so PHP's strstr, and other functions including strpos, do the reverse of taking a char/string and accepting an int. They then took it a step further and said anything that isn't a string or int gets converted to an int before it then becomes a character.

    Goes without saying that had it been implemented nowadays they definitely would not have done it that way and would have gone with the standard int->string typecasting practice instead - anyone wanting to pass an int and get the C behavior can just go die in a fire instead.

IMN logo majestic logo threadwatch logo seochat tools logo