Re: Arrays as pointers?
| From: | David Robley | Date: | Fri, 25 Jan 2002 01:15:10 +0000 |
| Subject: | Re: Arrays as pointers? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-82029@lists.php.net to get a copy of this message | ||
In article <NFBBJIEHOMPJBKCCIPKCIECDCBAA.v0idnull@liquidgrin.com>,
v0idnull@liquidgrin.com says...
> A friend of mine showed me this code recently.
>
> function firstLogin_string() {
> mt_srand(make_seed());
> $pool = "AaBbCcDdEeFfGgHhIiJjKkLlM";
> $length = 26;
> for($i=0; $i < $length; $i++) {
> $key .= $pool[mt_rand(0,strlen($pool)-1)];
> }
> return $key;
> }
>
> look at the for loop. $pool is not an array but is being treated as such and
> the key happens to be the position in $pool and it'll take that letter and
> save it to $key. I've never seen this done before. What is this method
> called and what other neat tricks can you do in respect to this?
>
This is documented behaviour - from the manual:
String access by character
Characters within strings may be accessed by specifying the zero-based
offset of the desired character after the string in curly braces.
Note: For backwards compatibility, you can still use the array-braces.
However, this syntax is deprecated as of PHP 4.
Use limitd only by your imagination :-)
--
David Robley
Temporary Kiwi!