Jump to content
xisto Community
coolcat50

Script Not Working I dont know why.

Recommended Posts

For some reason my random string script is not working. I got a fatal error when I tried it under XAMPP. I do not know why. It looks syntatically correct. Could someone help me?Here is the script: (Warning its over 100 lines long)

<?php//This PHP script will generate a random array and turn it into a string consisting of 0-9 and A-Z.//	This is the first developmental version.//Create 10 item array for string$string = array(0,0,0,0,0,0,0,0,0,0);//Create function to replace 10-36 with A-Zfunction conToStr() {	for ($a = 0;$a<=9;$i++) {		switch($string[$a]) {			case 10:				$string[$a] = 'a';				break;			case 11:				$string[$a] = 'b';				break;			case 12:				$atring[$a] = 'c';				break;			case 13:				$string[$a] = 'd';				break;			case 14:				$string[$a] = 'e';				break;			case 15:				$string[$a] = 'f';				break;			case 16:				$string[$a] = 'g';				break;			case 17:				$string[$a] = 'h';				break;			case 18:				$string[$a] = 'i';				break;			case 19:				$string[$a] = 'j';				break;			case 20:				$string[$a] = 'k';				break;			case 21:				$string[$a] = 'l';				break;			case 22:				$string[$a] = 'm';				break;			case 23:				$string[$a] = 'n';				break;			case 24:				$string[$a] = 'o';				break;			case 25:				$string[$a] = 'p';				break;			case 26:				$string[$a] = 'q';				break;			case 27:				$string[$a] = 'r';				break;			case 28:				$string[$a] = 's';				break;			case 29:				$string[$a] = 't';				break;			case 30:				$string[$a] = 'u';				break;			case 31:				$string[$a] = 'v';				break;			case 32:				$string[$a] = 'w';				break;			case 33:				$string[$a] = 'x';				break;			case 34:				$string[$a] = 'y';				break;			case 35:				$string[$a] = 'z';				break;			default:				//Number is 0-9				//We just keep it as it is				$string[$a] = $string[$a];		}	}}							//Create rand number loop for array sections and insert rand num into array.for ($i=0; $i<=9; $i++) {	//Start rand function	$string[$i] = rand(0,35);}//Perform the conToStr() function with no argumentsconToStr();//Put the array into another variable as a string$product = implode('',$string);//Echo out the stringecho $product?>

Share this post


Link to post
Share on other sites

The variable $string is created outside of the function conToStr(). This means that conToStr() cannot actually access that variable. I assume this is where you are getting some errors. You'd need to either make that variable global, define it within the function, or pass it to the function as an argument.

 

Secondly, the for loop inside conToStr() will run endlessly, hence the fatal error. You want it to stop when $a reaches 10, yet you are increasing the variable $i. Not only does $i not exist (so you can't increment it), but $a is never actually increasing, so the loop never ends. PHP aborts it after 30 seconds of going in an infinite loop.

 

This section...

 

//Create rand number loop for array sections and insert rand num into array.for ($i=0; $i<=9; $i++) {	//Start rand function	$string[$i] = rand(0,35);}

...is pointless. You never use the variable $string, so putting 10 random numbers in it is a waste of time - they never actually get used as they are not passed to the function.

 

The key point is that the function can only use variables that are...

Globals

Defined within the function itself

Passed to the function as arguments


Share this post


Link to post
Share on other sites

I think you might have just fixed my script. Thanks Rvalkass!Also, I use the $string array to get rand numbers and replace 10-35 with letters. That is the only way I know how to randomize letters.EDIT:I made the $string array global inside of the function and changed the $i++ to $a++ and it now works.Completed script:

<?php//This PHP script will generate a random array and turn it into a string consisting of 0-9 and A-Z.//	This is the first developmental version.//Create 10 item array for string$string = array(0,0,0,0,0,0,0,0,0,0);//Create function to replace 10-36 with A-Zfunction conToStr() {	global $string;	for ($a = 0;$a<=9;$a++) {		switch($string[$a]) {			case 10:				$string[$a] = 'a';				break;			case 11:				$string[$a] = 'b';				break;			case 12:				$atring[$a] = 'c';				break;			case 13:				$string[$a] = 'd';				break;			case 14:				$string[$a] = 'e';				break;			case 15:				$string[$a] = 'f';				break;			case 16:				$string[$a] = 'g';				break;			case 17:				$string[$a] = 'h';				break;			case 18:				$string[$a] = 'i';				break;			case 19:				$string[$a] = 'j';				break;			case 20:				$string[$a] = 'k';				break;			case 21:				$string[$a] = 'l';				break;			case 22:				$string[$a] = 'm';				break;			case 23:				$string[$a] = 'n';				break;			case 24:				$string[$a] = 'o';				break;			case 25:				$string[$a] = 'p';				break;			case 26:				$string[$a] = 'q';				break;			case 27:				$string[$a] = 'r';				break;			case 28:				$string[$a] = 's';				break;			case 29:				$string[$a] = 't';				break;			case 30:				$string[$a] = 'u';				break;			case 31:				$string[$a] = 'v';				break;			case 32:				$string[$a] = 'w';				break;			case 33:				$string[$a] = 'x';				break;			case 34:				$string[$a] = 'y';				break;			case 35:				$string[$a] = 'z';				break;			default:				//Number is 0-9				//We just keep it as it is				$string[$a] = $string[$a];		}	}}							//Create rand number loop for array sections and insert rand num into array.for ($i=0; $i<=9; $i++) {	//Start rand function	$string[$i] = rand(0,35);}//Perform the conToStr() function with no argumentsconToStr();//Put the array into another variable as a string$product = implode('',$string);//Echo out the stringecho $product?>

Edited by coolcat50 (see edit history)

Share this post


Link to post
Share on other sites

If the array of values is fixed, never changing, try this code on for size.
It uses the shuffle function to randomize the array so you save the switch statement.

<?php// define the starting array and the output variable$input = array( '0','1','2','3','4','5','6','7','8','9','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' );$out = '';// randomize the array of values	shuffle($input);	// loop through the required number of array values, concatenate the value to the output variablefor ($i = 1; $i <= 9; $i++) {	$out .= $input[$i];}// output the $output hereecho $out;?>
Might make a fair Password Generator??? Adjust the length from inside the for loop to add characters, possibly Uppercase, as well???

Share this post


Link to post
Share on other sites

Hmm yeah! I may use yours if mine becomes too long, but I want to see if I can make mine do some cool stuff. It will look more professional if it's longer. Laugh out loud!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.