SitePoint Sponsor

User Tag List

View Poll Results: Do you use curly or square brackets to access the characters in a string?

Voters
44. You may not vote on this poll
  • I use curlies: $str{3}

    16 36.36%
  • I use squares: $str[3]

    21 47.73%
  • I didn't know you could use either! / I use substr!

    7 15.91%
Page 2 of 2 FirstFirst 12
Results 26 to 41 of 41
  1. #26
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX
    At the moment, {} === [] (you can use curlies on arrays too)
    This is not exactly true, to be pedantic, there are some minor differences. But the fact is that [] is more powerful and always has been (internally) preferred over {}. Why did they deprecate [] in manual remains a mystery for ever...

  2. #27
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    This is not exactly true, to be pedantic, there are some minor differences.
    Are those internal things or something that would make a difference to what a script does?

    Douglas
    Hello World

  3. #28
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unlike [], curlies don't get interpolated ("foo $someVar{10} bar") and there is no {} operator (although [] doesn't work for strings too).

    Internally it's just an extra useless call:

    Code:
    void fetch_string_offset(...)
    {
    	fetch_array_dim(...);
    }

  4. #29
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used to use {} curly braces, but I suppose I will go to square brackets. It would be useful if we could mix substr in with the brackets, so we can go $string[0:15] instead of calling substring($string, 0, 15), much like in Python.

  5. #30
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks st, didn't know that.

    Quote Originally Posted by Ryan Wray
    much like in Python.
    And I think much like in Ruby, but it's the same feature request that got turned down

    OT: in Python, is $string[0:15] a method call? I know that it is is Ruby - I'm wondering if the reason for not having it in PHP is partially because it would just be harder to do internally, as the method calling behaviour isn't already built in.

    Cheers,
    Douglas
    Hello World

  6. #31
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was was just reading http://www.php.net/~derick/meeting-n...cleanup-for-vs, seems they do plan to add the substring functionality I mentioned earlier. Hurray!

  7. #32
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ryan Wray
    It would be useful if we could mix substr in with the brackets, so we can go $string[0:15] instead of calling substring($string, 0, 15).
    Does it really matter, if we would use $string[0:15] or substring($string, 0, 15). They are just doing the same.

    If a cat hunts the mouses, does it matter, if it's black or white

  8. #33
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It doesn't matter, it is just convient. It like saying is there any use of being to be able to access 1 character individually when you can always substring($string, 5, 1) to get the character at position 5. I know which I find more attractive.

    Another thing they are adding is concatentation of strings using [], ie:
    PHP Code:
    <?php
    $string 
    "hell";
    $string[] = "o";
    echo 
    $string// outputs hello
    ?>
    The use of this seems small, I guess it adds a bit of polymorphism between arrays and strings, if that is seen as useful to someone. I would still just use the standard concatenation operator though.

  9. #34
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If $string[0:15] is implemented, would it mean, that it would work with any array. ???

    Well, this would be really convenient.

  10. #35
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Squares all the way for me.

  11. #36
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by REMIYA
    If $string[0:15] is implemented, would it mean, that it would work with any array. ???

    Well, this would be really convenient.
    Yes, that is what it does mean. Basically, when working with strings, substr would be used, and when working with arrays, array_slice would be used.

  12. #37
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's really pity that they're not going to make this work on the left side.
    $foo[1, 5] = "x" would be useful.

  13. #38
    get into it! bigduke's Avatar
    Join Date
    May 2004
    Location
    Australia
    Posts
    847
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Honestly speaking, with the kind of systems I work on, I don't get to extract characters from strings at all, given the opportunity it'd be squares because I come from a C background

  14. #39
    SitePoint Addict Quaint's Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use [], because as someone mentioned before: I think of an array when I use this functionality and therefore [] makes more sense...

    Furthermore a small note: I suspect that using $str[x] or $str{x} will, behind the scenes, do exactly the same as substr($str,x,1) and therefore performance-wise makes no difference. So then it comes down to the question of maintainability/readability and then I think that eventhough $str[x] is pretty typical for PHP it's an easier read and easier to understand when someone's quickly browsing the code.. Furthermore $str[x] requires less typing and thus, lazy as I am, has preference anyway

    Quaint Tech
    - Blog on web development and web technology.

  15. #40
    SitePoint Addict lmasi02's Avatar
    Join Date
    Aug 2004
    Location
    Zambia
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i use [] i always thing that they are array. especialyy having a BG of java
    Power of Knowledge

  16. #41
    Spacebug Beansprout's Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    464
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Squares here...I use curlies to avoid concatenation issues.
    Thermal Degree - web design with standards! (View our portfolio)
    Vidahost - shared and reseller linux hosting with real support
    Use my free file uploader!
    5.99 .com/net/org/biz/us/name domains; 2.99 .info!


Bookmarks

Posting Permissions

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