fortnox007 Posted September 18, 2010 Share Posted September 18, 2010 Hi all, I just saw a small snippet on the internet and I wasn't sure on the { and } right after the variable. In the snippet below they have a variable that uses it right behind it. So to make myself more clear I know what's going on inside the {..} but I have never seen those things right after a variable. The closest thing I could think of is that it's a form of sub string. If anyone knows what it is and how to apply it on future cases I would love to hear it. Here is a piece of the code (which is part of a while loop) $string = $chars{mt_rand(0,strlen($chars))}; Quote Link to comment https://forums.phpfreaks.com/topic/213749-does-anyone-know-how-i-should-interpret-this/ Share on other sites More sharing options...
Username: Posted September 18, 2010 Share Posted September 18, 2010 Hi all, I just saw a small snippet on the internet and I wasn't sure on the { and } right after the variable. In the snippet below they have a variable that uses it right behind it. So to make myself more clear I know what's going on inside the {..} but I have never seen those things right after a variable. The closest thing I could think of is that it's a form of sub string. If anyone knows what it is and how to apply it on future cases I would love to hear it. Here is a piece of the code (which is part of a while loop) $string = $chars{mt_rand(0,strlen($chars))}; Not a clue either, I've only seen brackets, maybe it's the same thing as $string = $chars { mt_rand(0,strlen($chars)); } ??? :shrug: Quote Link to comment https://forums.phpfreaks.com/topic/213749-does-anyone-know-how-i-should-interpret-this/#findComment-1112527 Share on other sites More sharing options...
fortnox007 Posted September 18, 2010 Author Share Posted September 18, 2010 Hmm I just tested a little and i think it is indeed a form of sub string or something close $string_lala = 'abcd'; echo $string_lala{0}; // gives an a Hmm could be like an array also :-) Quote Link to comment https://forums.phpfreaks.com/topic/213749-does-anyone-know-how-i-should-interpret-this/#findComment-1112529 Share on other sites More sharing options...
Alex Posted September 18, 2010 Share Posted September 18, 2010 You can access the characters of a string by using brackets like that, but it's depreciated as of PHP 6 in favor of the [] operator. $chars = "abc"; echo $chars{2}; // c // But you should do this: echo $chars[2]; Quote Link to comment https://forums.phpfreaks.com/topic/213749-does-anyone-know-how-i-should-interpret-this/#findComment-1112530 Share on other sites More sharing options...
fortnox007 Posted September 18, 2010 Author Share Posted September 18, 2010 HA nice, Thx Alex! So the code should be rewritten like $string = $chars[mt_rand(0,strlen($chars))]; // as of php6 Quote Link to comment https://forums.phpfreaks.com/topic/213749-does-anyone-know-how-i-should-interpret-this/#findComment-1112531 Share on other sites More sharing options...
Alex Posted September 18, 2010 Share Posted September 18, 2010 It should be written that way as of now. The [] operator will work for accessing characters of a string before PHP 6, and because it'll be the only non-depreciated way as of PHP 6 it's a good idea to only use [] now. Unless you want your code to break on a server running PHP 6. Quote Link to comment https://forums.phpfreaks.com/topic/213749-does-anyone-know-how-i-should-interpret-this/#findComment-1112532 Share on other sites More sharing options...
fortnox007 Posted September 18, 2010 Author Share Posted September 18, 2010 Thanks for the advise! Quote Link to comment https://forums.phpfreaks.com/topic/213749-does-anyone-know-how-i-should-interpret-this/#findComment-1112534 Share on other sites More sharing options...
fortnox007 Posted September 18, 2010 Author Share Posted September 18, 2010 hehe Just made a noobie function to echo all characters out on a new line. If someone can use it.. (i doubt that, but it's nice to fill up your screen) $stringy = 'monkeys tend to eat banana\'s'; $length = strlen($stringy); for ($i=0;$i<$length;$i++){ echo $stringy[$i].'<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/213749-does-anyone-know-how-i-should-interpret-this/#findComment-1112539 Share on other sites More sharing options...
Alex Posted September 18, 2010 Share Posted September 18, 2010 echo implode("<br />", str_split($str)); Quote Link to comment https://forums.phpfreaks.com/topic/213749-does-anyone-know-how-i-should-interpret-this/#findComment-1112542 Share on other sites More sharing options...
fortnox007 Posted September 18, 2010 Author Share Posted September 18, 2010 NIce, I keep forgetting to look into implode and explode. Thanks for sharing this Quote Link to comment https://forums.phpfreaks.com/topic/213749-does-anyone-know-how-i-should-interpret-this/#findComment-1112553 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.