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 1 of 2 12 LastLast
Results 1 to 25 of 41
  1. #1
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Do you use $str{3} or $str[3] to access the characters in a string?

    Do you use $str{3} or $str[3] to access the characters in a string?

    There's been some discussion on the internals list about this, and I'minterested in what the average SitePoint PHP'er uses

    Douglas
    Hello World

  2. #2
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like squares . Let's don't forget, that the PHP string is just an array of characters. Just like the old C++.

  3. #3
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Both in PHP. [3] in Ruby, so I try to do [3] in PHP too to make things more consistent.

  4. #4
    Internet Business Optimizer KCgame's Avatar
    Join Date
    Mar 2005
    Location
    Singapore
    Posts
    935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use the squares
    Any major issues between the two?

  5. #5
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using squares could lead to confussion.

  6. #6
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used to use {}, but having read this I see no reason to do so anymore.

  7. #7
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always think that its an array if I use []...
    Why's (Poignant) Guide to Ruby
    learn ruby with foxes, wizards, and chunky bacon

  8. #8
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always use substr($str,2,1)
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  9. #9
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always use square brackets to find charecters in a string becuase they are commonly used to find values in an array.

  10. #10
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Brooklyn, NY
    Posts
    359
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Curly braces were the preferred format until recently. Now square brackets are the preferred format, and the use of curly braces will begin to generate an E_STRICT error with 5.1. That's too bad, since I've tried to teach the "preferred" way in all of the classes I've taught over the past few years.

    At least now the preference is more definitive rather than just being mentioned in the manual.
    Chris Shiflett
    http://shiflett.org/

  11. #11
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Curlies or substr. Whatever I'm in the mood for.

    Quote Originally Posted by php.net/string
    String access and modification by character

    Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string in curly braces.

    Note: For backwards compatibility, you can still use array-brackets for the same purpose. However, this syntax is deprecated as of PHP 4.

    Example 11-5. Some string examples
    PHP Code:
    <?php
    // Get the first character of a string
    $str 'This is a test.';
    $first $str{0};

    // Get the third character of a string
    $third $str{2};

    // Get the last character of a string.
    $str 'This is still a test.';
    $last $str{strlen($str)-1};

    // Modify the last character of a string
    $str 'Look at the sea';
    $str{strlen($str)-1} = 'e';
            
    ?>
    Let me get this straight.

    [] was proper in 3.
    [] was deprecated in 4 in preference of {}.
    {} was deprecated in 5.1 in preference of [].

    Is that right? I think I'll use substr.

  12. #12
    SitePoint Enthusiast shref's Avatar
    Join Date
    Nov 2004
    Location
    Egypt, Alexandria
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    substr is the best
    Shreef
    blog: shreef.com
    twitter: @shreef

  13. #13
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used {} because I thought that was the prefered way, (and I thought it was string specific operator so was more clear what you were intending todo) until I realised the change by the flood of posts on phpdev list.

    Be interesting what will happen in PHP6, with UTF8 strings... do you want a character, or a byte value..

  14. #14
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, surprised a third use {}, I was expecting it to be lower than that.

    Cheers,
    Douglas
    Hello World

  15. #15
    SitePoint Addict
    Join Date
    Aug 2002
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't believe they would raise an E_STRICT error for that in 5.1. I am sure many people will not like that. They should just silently deprecate it.

  16. #16
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always used {} because I tried to be a good php citizen and obey what they say. And now I'm punished for that once again. Why do they love me that much?

  17. #17
    SitePoint Evangelist djdykes's Avatar
    Join Date
    Feb 2005
    Location
    Chester, Cheshire
    Posts
    565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [ ] sqaures all the way........

    i only use {$variable} on variables

  18. #18
    SitePoint Addict
    Join Date
    Mar 2005
    Location
    India
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use either substring or []. I wasn't aware of {} before I read this post.
    Thanks DougBTX. But {} will be the thing of the past soon.

  19. #19
    SitePoint Guru
    Join Date
    Nov 2002
    Posts
    841
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been using the curlies. Was a reason given for the switch?

  20. #20
    SitePoint Zealot
    Join Date
    Feb 2005
    Posts
    195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [] mainly because I use a few languages during the course of the day, and that works in pretty much all of them
    Set up a free Instant Arcade!!
    Find the domainname you want at ExpireWorks.com
    Free $6.95 directi account

  21. #21
    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 Selkirk
    I've been using the curlies. Was a reason given for the switch?
    I don't believe there's any technical reason, just one way of making the language less ambiguous. At the moment, {} === [] (you can use curlies on arrays too) so I guess it's just an idea to standardise on one. I'm up for that, I've been a bad PHP citezen and just carried on using the one which looked better

    Douglas
    Hello World

  22. #22
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX
    I don't believe there's any technical reason, just one way of making the language less ambiguous. At the moment, {} === [] (you can use curlies on arrays too) so I guess it's just an idea to standardise on one. I'm up for that, I've been a bad PHP citezen and just carried on using the one which looked better

    Douglas
    I believe that early on, the reason for {} was to make it easier for the parser, but apperantly that is no longer considered a valid technical reason.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  23. #23
    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 sweatje
    I believe that early on, the reason for {} was to make it easier for the parser, but apperantly that is no longer considered a valid technical reason.
    Yep, that's what I understood too.
    Hello World

  24. #24
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Brooklyn, NY
    Posts
    359
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kabatak
    I can't believe they would raise an E_STRICT error for that in 5.1. I am sure many people will not like that. They should just silently deprecate it.
    That would be very annoying, and it goes against the whole point of deprecating something. You may as well just surprise everyone and suddenly change the syntax. :-)

    (The point of E_STRICT is to aid developers with forward compatibility.)
    Chris Shiflett
    http://shiflett.org/

  25. #25
    SitePoint Addict
    Join Date
    Aug 2002
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does PHP throw an E_STRICT right now with PHP 5 < 5.1 if you use $str[1] ?


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
  •