joesaddigh Posted March 12, 2009 Share Posted March 12, 2009 I have stole a function from the net that generates a random password put it in its own .php file and am including it in my script. The random password prints to the screen but the password variable is empty. Does anybody have any idea what the problem could be? The code is as follows: The calling script include "generatepassword.php"; $password = generatePassword(); echo $password; The function <?php function generatePassword ($length = { // start with a blank password $password = ""; // define possible characters $possible = "0123456789bcdfghjkmnpqrstvwxyz"; // set up a counter $i = 0; // add random characters to $password until $length is reached while ($i < $length) { // pick a random character from the possible ones $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); // we don't want this character if it's already in the password if (!strstr($password, $char)) { $password .= $char; $i++; } } echo "The random password generated is " . $password; } ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/149124-solved-variable-scope/ Share on other sites More sharing options...
lonewolf217 Posted March 12, 2009 Share Posted March 12, 2009 the function doesn't return a variable, it just echos it. your main PHP script has no knowledge of the variables within the script, so you cannot echo it outside of the function. change it to something like this <?php function generatePassword ($length = { // start with a blank password $password = ""; // define possible characters $possible = "0123456789bcdfghjkmnpqrstvwxyz"; // set up a counter $i = 0; // add random characters to $password until $length is reached while ($i < $length) { // pick a random character from the possible ones $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); // we don't want this character if it's already in the password if (!strstr($password, $char)) { $password .= $char; $i++; } } echo "The random password generated is " . $password; return $password; } ?> and I think that will work Quote Link to comment https://forums.phpfreaks.com/topic/149124-solved-variable-scope/#findComment-783025 Share on other sites More sharing options...
joesaddigh Posted March 12, 2009 Author Share Posted March 12, 2009 Yes! Thanks it works! Is there a way of making variables global. If you want to use variables in different .php files how do you do this. Do you need to declare them differently? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/149124-solved-variable-scope/#findComment-783027 Share on other sites More sharing options...
lonewolf217 Posted March 12, 2009 Share Posted March 12, 2009 global variables are frowned upon from my understanding. There is a way to use them, but I dont know how since I haven't used them before Quote Link to comment https://forums.phpfreaks.com/topic/149124-solved-variable-scope/#findComment-783032 Share on other sites More sharing options...
premiso Posted March 12, 2009 Share Posted March 12, 2009 Is there a way of making variables global. If you want to use variables in different .php files how do you do this. Do you need to declare them differently? Why would you want it global? There is no reason, just use it like you have: $password = generatePassword(); That is the point of functions, I would remove the echo $password in the function, as you do not need to echo it. Quote Link to comment https://forums.phpfreaks.com/topic/149124-solved-variable-scope/#findComment-783036 Share on other sites More sharing options...
joesaddigh Posted March 12, 2009 Author Share Posted March 12, 2009 Ok your right i will use functions for this in future! The echo password was there for debugging only. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/149124-solved-variable-scope/#findComment-783040 Share on other sites More sharing options...
rhodesa Posted March 12, 2009 Share Posted March 12, 2009 to answer you question...yes...you can with: <?php function foobar ( ) { global $myvar; } ?> but as everyone else stated...the need for global variables is few and far between and this is definitely not one of those cases. Quote Link to comment https://forums.phpfreaks.com/topic/149124-solved-variable-scope/#findComment-783042 Share on other sites More sharing options...
joesaddigh Posted March 12, 2009 Author Share Posted March 12, 2009 Thanks mate Quote Link to comment https://forums.phpfreaks.com/topic/149124-solved-variable-scope/#findComment-783054 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.