Xeoncross Posted January 16, 2008 Share Posted January 16, 2008 If the title is confusing you might want to take a look at this: <?php function random_charaters($number) { $ANC = 'ACEFGHJKLMNPRSTUVWXY345679'; $chars = null; for($i=0; $i<$number; $i++) { $chars .= $ANC{rand(0,26)}; //<-- Take a look at this } return $chars; } //returns something like: A3GMN99 ?> Now when I use this function PHP throws a E_NOTICE error. The function works fine - but I don't like to go against PHP. So that brings me to the topic of how should I properly use "variable arrays"? (or whatever you call them: Variable Variables) Link to comment https://forums.phpfreaks.com/topic/86255-solved-variable-arrays-without-an-array/ Share on other sites More sharing options...
trq Posted January 16, 2008 Share Posted January 16, 2008 Theres not really such a thing as variable arrays and your not using variable variables. Your simply trying to access indexes of a string. What version of php are you using? Use [] instead. eg; $chars .= $ANC[rand(0,26)]; Link to comment https://forums.phpfreaks.com/topic/86255-solved-variable-arrays-without-an-array/#findComment-440590 Share on other sites More sharing options...
Daukan Posted January 16, 2008 Share Posted January 16, 2008 It should throw a random 'Uninitialized string offset' error. The max value in the rand() function is larger than the amount of values in the $ANC array. It should be 25. Link to comment https://forums.phpfreaks.com/topic/86255-solved-variable-arrays-without-an-array/#findComment-440592 Share on other sites More sharing options...
trq Posted January 16, 2008 Share Posted January 16, 2008 Your simply trying to access indexes of a string. Sorry, I should say... Your simply trying to access string elements via an index. Link to comment https://forums.phpfreaks.com/topic/86255-solved-variable-arrays-without-an-array/#findComment-440593 Share on other sites More sharing options...
Xeoncross Posted January 16, 2008 Author Share Posted January 16, 2008 The max value in the rand() function is larger than the amount of values in the $ANC array. It should be 25. Now I know I need to go to bed... thanks Daukan. Link to comment https://forums.phpfreaks.com/topic/86255-solved-variable-arrays-without-an-array/#findComment-440601 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.