String reference type

what does {} do when next to a string variable

Page 1 of 1

4 Replies - 959 Views - Last Post: 18 September 2009 - 06:50 AM Rate Topic: -----

#1 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

String reference type

Post icon  Posted 16 September 2009 - 06:43 AM

Alright i have a ForEach loop in PHP that i am trying to understand what is going on. I understand the ForEach loop statement, but the assignment variable has a reference type signature placed on it and i cant seem to find what it does.

matches is an array that has had a preg_match_all method performed to it.

...
foreach( $matches[1] as $str ) 
{
...
if( $str{0} < "\x80" ) <------
...
}



First question i guess is why does the matches variable reference the first index everytime in the foreach loop? I assumed this was because they only wanted this loop to go through 1 time but im not quite sure.

Secondly, what is this variable reference type being used on the variable $str?

I havent seen anything like this before, $str{0} , if it was parenthesis () or square brackets [], i would understand what it was supposed to be doing, but the curly brackets {} im not quite sure.

At first i assumed that it was a character byte reference but everything there after points to a string segment instead of a character reference.

This post has been edited by woodjom: 16 September 2009 - 06:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: String reference type

#2 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: String reference type

Posted 16 September 2009 - 08:58 AM

$matches[1] will referance the 2nd data stored in the array matches, with $matches[0] getting the first one. if you take of the square [] then it woul loop all.

im guessing the curly braces are a typo, does the script work. ?
Was This Post Helpful? 0
  • +
  • -

#3 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: String reference type

Posted 17 September 2009 - 08:23 AM

yes the script works, as far as i know.

As i am not really a PHP programmer but i have done a good bit of translation on this site code so far. I have developed an interpretation eye from PHP to ASP.Net. As of right now i am 20% completed on the site translation so far but this has thrown me for a loop.

Is their a shortcut way of referencing a Character in a String? Most of the time it seems there is a function to strip out a character at a specified location but i have also seen ways where PHP will have a shortcut annotation to do the same bit of logic without specifically typing the process out. Most of the time it is based on a GNU build so far in this site and as such is not always documented on the PHP reference manual.

Lastly, since the $matches[1] only references the second segement of the array ( i call it the first element), i assume that the foreach loop would only iterate once?
Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: String reference type

Posted 17 September 2009 - 08:43 AM

It's just an array indexer, exactly the same as [], not a typo.

Quote

Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 5.3.0. Use square brackets instead, such as $str[42].


View Postwoodjom, on 17 Sep, 2009 - 07:23 AM, said:

Lastly, since the $matches[1] only references the second segement of the array ( i call it the first element), i assume that the foreach loop would only iterate once?

Regarding this, if it's a preg_match_all, $matches[1] will contain all of the matched items of the first capturing group.
Was This Post Helpful? 0
  • +
  • -

#5 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: String reference type

Posted 18 September 2009 - 06:50 AM

kewlio, as i dont know the age of this site, it was probably pre-php 5.3 and looks like it has some post 5.3 development with the new standard notation. Ok thanx for this bit.

View PostMageUK, on 17 Sep, 2009 - 09:43 AM, said:

It's just an array indexer, exactly the same as [], not a typo.

Quote

Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 5.3.0. Use square brackets instead, such as $str[42].


on this little snippet. So what your saying is that the foreach loop will actually go through each and every character in the matched string? or the loop will only go through one loop?

As i have been digesting this little bit of code, the innards of the foreach seem to iterate multiple times through the matched string, hence why im kinda of confused on the real need for the foreach loop.

View PostMageUK, on 17 Sep, 2009 - 09:43 AM, said:

View Postwoodjom, on 17 Sep, 2009 - 07:23 AM, said:

Lastly, since the $matches[1] only references the second segement of the array ( i call it the first element), i assume that the foreach loop would only iterate once?

Regarding this, if it's a preg_match_all, $matches[1] will contain all of the matched items of the first capturing group.


Sorry if i seem to not get the responses but i do understand the responses you have given. I just tend to want to get more detailed information about the reasons and how to's. I have done various searches on the PHP and Google, and there are alot of instructions but very little explanations (deep reasons) as to why you would do something like this, in regards to the foreach loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1