srcfresno Posted August 25, 2011 Share Posted August 25, 2011 So I found this code from someone else on here who posted about creating a random string. I've copied the code for myself but when I upload php page to my server and view source... there is no php code. Essentially, i see no randomly generated text. what am i missing? <?php function genRandomString() { $length = 5; $characters = array_merge(range(0,9),range('a','z'),range('A','Z')); $string = ""; for ($p = 0; $p < $length; $p++) { $string .= $characters[mt_rand(0, count($characters)-1)]; } return $string; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245717-generate-random-string/ Share on other sites More sharing options...
Pikachu2000 Posted August 25, 2011 Share Posted August 25, 2011 Post the code in which you call the function also. Quote Link to comment https://forums.phpfreaks.com/topic/245717-generate-random-string/#findComment-1262063 Share on other sites More sharing options...
srcfresno Posted August 25, 2011 Author Share Posted August 25, 2011 can you be a little more descriptive please Quote Link to comment https://forums.phpfreaks.com/topic/245717-generate-random-string/#findComment-1262065 Share on other sites More sharing options...
ujaval Posted August 25, 2011 Share Posted August 25, 2011 You can see only HTML and javascript when you view source the page in the browser. PHP is a server side scripting language. PHP code can't be shown Post the code where you call the function genRandomString(). Quote Link to comment https://forums.phpfreaks.com/topic/245717-generate-random-string/#findComment-1262066 Share on other sites More sharing options...
Pikachu2000 Posted August 25, 2011 Share Posted August 25, 2011 Post the code that you've written that makes a call to the function above, for the purpose of generating the output. Quote Link to comment https://forums.phpfreaks.com/topic/245717-generate-random-string/#findComment-1262067 Share on other sites More sharing options...
srcfresno Posted August 25, 2011 Author Share Posted August 25, 2011 excuse my ignorance... so like that <?php function genRandomString($length, $characters, $string) { $length = 5; $characters = array_merge(range(0,9),range('a','z'),range('A','Z')); $string = ""; for ($p = 0; $p < $length; $p++) { $string .= $characters[mt_rand(0, count($characters)-1)]; } return $string; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245717-generate-random-string/#findComment-1262068 Share on other sites More sharing options...
joecooper Posted August 26, 2011 Share Posted August 26, 2011 then use echo($genRandomString(5, "a-z"); Quote Link to comment https://forums.phpfreaks.com/topic/245717-generate-random-string/#findComment-1262073 Share on other sites More sharing options...
xyph Posted August 26, 2011 Share Posted August 26, 2011 That function seems poorly written. Use something like this <?php $characters = array_merge(range(0,9),range('a','z'),range('A','Z')); shuffle( $characters ); // Where 5 is legnth of string echo implode( '', array_slice($characters,0,5) ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/245717-generate-random-string/#findComment-1262074 Share on other sites More sharing options...
srcfresno Posted August 26, 2011 Author Share Posted August 26, 2011 that totally worked, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/245717-generate-random-string/#findComment-1262084 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.