SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru babyboy808's Avatar
    Join Date
    Nov 2004
    Location
    dublin
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can Someone explain this tidbit of code please?

    Hi Guys,

    Grabbed some code to learn from and I understand all of it expect for one small part here:

    PHP Code:
    $string .= $characters[mt_rand(055)]; 
    $characters is not an array, so how is square brackets allowed?


    The rest if the code
    PHP Code:
    function genRandomString()
        {
            
    $length_of_string 16;
            
    $characters ='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            
    $string "";   

            for (
    $i 0$i $length_of_string$i++) {
                
    $string .= $characters[mt_rand(055)];
            }
            return 
    $string;
        }
       
        echo 
    genRandomString() 

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,036
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    $characters is a string. It has the "special" property of being like an "object".
    So rather than array[key] it means string[char@position]

    From the documentation http://us2.php.net/manual/en/language.types.string.php
    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 using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose.

  3. #3
    SitePoint Guru babyboy808's Avatar
    Join Date
    Nov 2004
    Location
    dublin
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect, thanks mate!


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
  •