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
| SitePoint Sponsor |
I use curlies: $str{3}
I use squares: $str[3]
I didn't know you could use either! / I use substr!





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
I like squares. Let's don't forget, that the PHP string is just an array of characters. Just like the old C++.




Both in PHP. [3] in Ruby, so I try to do [3] in PHP too to make things more consistent.




I use the squares![]()
Any major issues between the two?
Using squares could lead to confussion.
Location: Alicante (Spain)... Hot and Sunny...
Texas Holdem Poker Probability Calculator | DNS test
Avatars | English Spanish Translation | CAPTCHA with audio
Email | PHP scripts | Cruft free domain names | MD5 Cracker




I used to use {}, but having read this I see no reason to do so anymore.


I always think that its an array if I use []...
Why's (Poignant) Guide to Ruby
learn ruby with foxes, wizards, and chunky bacon
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.
I always use square brackets to find charecters in a string becuase they are commonly used to find values in an array.


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/





Curlies or substr. Whatever I'm in the mood for.
Let me get this straight.Originally Posted by php.net/string
[] 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.![]()
substr is the best![]()
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..![]()





Hmm, surprised a third use {}, I was expecting it to be lower than that.
Cheers,
Douglas
Hello World


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.





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?![]()



[ ] sqaures all the way........
i only use {$variable} on variables


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.




I've been using the curlies. Was a reason given for the switch?

[] 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





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 betterOriginally Posted by Selkirk
Douglas
Hello World
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.Originally Posted by DougBTX
Jason Sweat ZCE - jsweat_php@yahoo.com
Book: PHP Patterns
Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
Detestable (adjective): software that isn't testable.





Yep, that's what I understood too.Originally Posted by sweatje
Hello World


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. :-)Originally Posted by kabatak
(The point of E_STRICT is to aid developers with forward compatibility.)
Chris Shiflett
http://shiflett.org/


Does PHP throw an E_STRICT right now with PHP 5 < 5.1 if you use $str[1] ?
Bookmarks