SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: function ();

  1. #1
    SitePoint Zealot
    Join Date
    Jul 2000
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could anyone give me some help on functions?
    I have the following code which I picked up from someone, which is meant to make pronouncable passwords.
    My problem is, I don't know how to execute the function.
    Can anyone help?

    Code:
    <?
    $base_a1 = "aeiouy"; 
    $base_a2 = "bcdfghjklmnpqrstvwxz"; 
    $base_0 = "0123456789"; 
    
    function generate($name_length=8, $name_base=1)  {
    
    // name_base = 1 - alfa, 2 - num. 
    
    global $base_a1,$base_a2,$base_0; 
    
    $retval = ''; 
    
    mt_srand((double)microtime()*1000000); 
    
    switch ($name_base) 
    { 
    case 1: 
    { 
    $sw=false; 
    for ($p=0;$p<$name_length;$p++) 
    { 
    if ($sw) 
    $retval .= substr($base_a1,mt_rand(0,strlen($base_a1)-1),1); 
    else 
    $retval .= substr($base_a2,mt_rand(0,strlen($base_a2)-1),1); 
    $sw=!$sw; 
    } 
    break; 
    } 
    case 2: 
    { 
    for ($p=0;$p<$name_length;$p++) 
    $retval .= substr($base_0,mt_rand(0,strlen($base_0)-1),1); 
    break; 
    } 
    } 
    
    return $retval; 
    } 
    
    ?>
    Thanks,
    AJ

  2. #2
    SitePoint Member
    Join Date
    Feb 2001
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe..

    $password = generate();

  3. #3
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's nothing like internal documentation!

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2000
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cheers guys.
    although you did sound very sarcastic..
    -aj

  5. #5
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A helpful tip is to tell yourself that functions can either...

    A - Print out some text/code to be processed, such as calling some sort of site-wide error() function to print out a generic error message.

    ...or...

    B - Return a value. In this case, you'll notice this line is near the end of the function:

    Code:
    return $retval;
    Now, that basically means that whent he function is executed, it "returns" that value - so, how do you capture the value returned? You assign it to a variable, as recommended by kentaurus:

    Code:
    $password = generate();
    Any value returned by the function is placed in the $password variable.

    The thought of functions returning values confused me for the longest time, but it makes sense once you get used to it.

    Good luck!

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry you didn't like my attempt at humour. My mistake, I thought you wanted an explaination of the function's algorithm.

    Just to add to TWCommish's comments. As well as looking to see what you get back from a function
    Code:
    return $retval;
    You should also look at the function's "signature" to see what the function expects from you when you call it in your code. The signature for this function is
    Code:
    function generate($name_length=8, $name_base=1)
    This means that the function will receive two parameters. Fortunately, the writer used "meaningful" variable names so we can guess what the parameters signify. It seems that you can pass the function the "name_length" which would be the length of password you want generated and returned. Also, from this comment in the code,
    Code:
    // name_base = 1 - alfa, 2 - num.
    the second argument appears to allow you to specify whether you want the password generated to be alpha or numeric.

    So if you wanted the function to generate a 6 letter alpha password you would use
    Code:
    $password = generate( 6, 1 );
    If you wanted a ten digit numeric password
    Code:
    $password = generate( 10, 2 );
    However, you will notice that the writer of the function has actually specified "default" values for the two parameters

    Code:
    $name_length=8, $name_base=1
    This means that if you call the function in this manner
    Code:
    $password = generate( );
    without passing any parameters in the function call, the function will use the default values $name_length=8, $name_base=1 and you would expect to receive an alpha password 8 letters in length.

    Note you could also call the function like this
    Code:
    $password = generate(7);
    This would override the default value of $name_length and assign $name_length = 7, but keep the default value of $name_base=1 because you didn't provide a second arguement in the function call.

    Hope that helps rather than confuses.
    Last edited by freakysid; Feb 10, 2001 at 17:37.


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
  •