Jump to content

Function returns nothing.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
14 replies to this topic

#1
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
I've tried changing a number of things and asked a friend but I'm stumped: this function I wrote should return a string, but instead it seems to ignore the for loop.
Perhaps someone here could tell me what I've done wrong?

    mt_srand(time());
    
    $letters=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
                "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
                "0","1","2","3","4","5","6","7","8","9",
                "10","13","1337","2008","fail","wat","n00b","fake","luvz","php",
                "_");
    $letno=(count($letters)-1);
    
    function cstr($length, $randlen){
        $string="";
        if ($randlen){
            $length=mt_rand(1, $length);
        }
        for ($i=0; $i < ($length+1); $i++){
            $string=$string.$letters[(mt_rand(0,$letno))];
        }
        return $string;
    }
FYI, $randlen is a boolean, I tend to send true/false through; but have also tried 1/0 to no avail.
An example of how I call the function:
$email_address=cstr(20,true)."@".cstr(20,true).".".cstr(3,false);
This makes $email_address:
@.
Any ideas would be greatly appreciated.
Posted Image

#2
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
Put an 'echo' function in the for loop. If you see print xx times you know the for loop is being executed.

The reason I say this is because your variables, $letters and $letno, are out of scope. You need to declare them in the function with

global $letters, $letno;

You probably want to seed your rand in the function as well.

#3
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
Thanks, Lop, I'll give it a go.
Posted Image

#4
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
Worked like a charm, Lop! Well...certainly on my test version on localhost; so I'm assuming the same will apply to my larger script. Thanks very much.
Posted Image

#5
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
I'm afraid I've come accross a problem - most of the strings generated in one go seem to be identical or just a couple characters off identical...perhaps this is because the script runs very fast and the randomisation is seeded using time?
Any solution for this?
Posted Image

#6
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
Never mind, use of sleep() is sufficient for what I need it for.
Thanks for your help, Lop.
Posted Image

#7
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
You could also try PHP: microtime - Manual instead of time()

#8
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts

Lop said:

You could also try PHP: microtime - Manual instead of time()

Looks useful, thanks.
Posted Image

#9
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Just a note:

"As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically."

PHP: mt_srand - Manual

What version of PHP are you using?

#10
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
4.4.7 on my site's server.
Interesting, so I don't need to seed randomisation...
Thanks for the info.
Posted Image

#11
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
I forgot about that. It is weird that you seeded it (even though you didn't need to) and the number still came out close. If you don't seed and don't pause is there enough randomness?

#12
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
Well the results didn't matter a huge amount and I only needed to run the script once, so I haven't modified that script (yet). So I couldn't say.
Posted Image