Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Script Not Working, I don't know why.
coolcat50
post Feb 2 2008, 09:40 PM
Post #1


Super Member
*********

Group: [HOSTED]
Posts: 267
Joined: 5-October 07
From: Random Places
Member No.: 51,171
Spam Patrol



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)
CODE

<?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-Z
function 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 arguments
conToStr();

//Put the array into another variable as a string
$product = implode('',$string);

//Echo out the string
echo $product
?>
Go to the top of the page
 
+Quote Post
rvalkass
post Feb 3 2008, 10:15 AM
Post #2


apt-get moo
Group Icon

Group: [MODERATOR]
Posts: 2,029
Joined: 28-May 05
From: Hertfordshire, England
Member No.: 7,593
Spam Patrol



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...

CODE
//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
Go to the top of the page
 
+Quote Post
coolcat50
post Feb 3 2008, 06:31 PM
Post #3


Super Member
*********

Group: [HOSTED]
Posts: 267
Joined: 5-October 07
From: Random Places
Member No.: 51,171
Spam Patrol



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:
CODE

<?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-Z
function 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 arguments
conToStr();

//Put the array into another variable as a string
$product = implode('',$string);

//Echo out the string
echo $product
?>


This post has been edited by coolcat50: Feb 3 2008, 06:39 PM
Go to the top of the page
 
+Quote Post
jlhaslip
post Feb 3 2008, 07:23 PM
Post #4


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,844
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



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.

CODE
<?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 variable
for ($i = 1; $i <= 9; $i++) {
    $out .= $input[$i];
}

// output the $output here
echo $out;
?>

Might make a fair Password Generator??? Adjust the length from inside the for loop to add characters, possibly Uppercase, as well???
Go to the top of the page
 
+Quote Post
coolcat50
post Feb 3 2008, 07:28 PM
Post #5


Super Member
*********

Group: [HOSTED]
Posts: 267
Joined: 5-October 07
From: Random Places
Member No.: 51,171
Spam Patrol



Wow! My script is so complicated compared to that one. Well, I might make a password generator. Do you mind if I use your code snippet for it?
Go to the top of the page
 
+Quote Post
jlhaslip
post Feb 3 2008, 07:44 PM
Post #6


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,844
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



Nope, I don't mind at all.
Glad to assist.
maybe I'll make one and we can compare notes???
Go to the top of the page
 
+Quote Post
coolcat50
post Feb 3 2008, 08:08 PM
Post #7


Super Member
*********

Group: [HOSTED]
Posts: 267
Joined: 5-October 07
From: Random Places
Member No.: 51,171
Spam Patrol



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!
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Sata Hard Drive Not Recognized By Windows(11)
  2. Java Script Drop Down Menu With Css(2)
  3. Php Quiz Script(20)
  4. Toggle display in page script(8)
  5. Free Weather Feed Script(1)
  6. Test Your Php Pages W/o Upload/internet(57)
  7. [help] Java Script: Window.open(8)
  8. Guestbook (cgi-script) Problems(1)
  9. Problems With Outlook Express(6)
  10. Ipod Not Working, Need Help(15)
  11. Slow Connection With Linksys Wrt54g Router(6)
  12. Watermark Your Image With Simple Php Script(33)
  13. Free Myspace Script?!?(3)
  14. Javascript : No Right Click Script !@(12)
  15. Jsp Or Java Chat Script Like Mig33(4)
  1. Html Code Tester. Online Script(15)
  2. Invite Script..(1)
  3. Need Help Installing Dolphin Community Script!(5)
  4. Ftp Access Has Stopped Working All Of A Sudden [resolved](5)
  5. File Manager Not Working(2)
  6. Webmail Server Script(2)
  7. Help For My Seagate Sata Hard Drive!(7)
  8. Guessing Php Script(0)
  9. Help With Xp - Autoupdate Not Working [resolved](13)
  10. Backup Not Working [resolved](5)
  11. Xobni - Working With Mails Got Easier.(0)
  12. Adjusting Rows/cols Of Frames In Frameset Using Javascript Is Not Working In Firefox 3 Is Not Working(4)
  13. Flash Wmode(transparent) Pramater In Firefox3(on Linux) - Not Working(0)