rwwd Posted September 3, 2010 Share Posted September 3, 2010 Hi there people of the PHP forum, This functions intention is to output the same code 4 digit code twice before generating a new one, I can get it to do one fine, but I cannot for the life of me figure out how to get it to repeat twice before changing on the third call. private function KeyGen(){ $Numerical = range(0,9); $Alpha = range("A","Z"); $Keys = array_merge($Numerical, $Alpha); return $Keys[rand(0,35)].$Keys[rand(0,35)].$Keys[rand(0,35)].$Keys[rand(0,35)]; } I am needing it to do this so that each 'set' of 2 calls to the function (within the class) gives the same 4 digit code. At the moment I am just making one call & then assigning that single call to a variable and then referencing that var twice, this is not what I want to do, but it scratches the itch for now... Any idea's/other logic would be greatly appreciated. Cheers, Rw Quote Link to comment https://forums.phpfreaks.com/topic/212455-private-class-functionproperty-repeater-please/ Share on other sites More sharing options...
trq Posted September 4, 2010 Share Posted September 4, 2010 While there are ways of doing what you want, I would consider your logic a serious design floor. really, it is going to make your method seem unreliable. Quote Link to comment https://forums.phpfreaks.com/topic/212455-private-class-functionproperty-repeater-please/#findComment-1107046 Share on other sites More sharing options...
pornophobic Posted September 4, 2010 Share Posted September 4, 2010 While agreeing with thorpe, I guess what you could do is pass the second pin as a function property and if it's set it just repeats whatever you passed previously. function KeyGen($pin = ''){ $Numerical = range(0,9); $Alpha = range("A","Z"); $Keys = array_merge($Numerical, $Alpha); $Gen = $Keys[rand(0,35)].$Keys[rand(0,35)].$Keys[rand(0,35)].$Keys[rand(0,35)]; if ($pin != '') { return $pin; } else { return $Gen; } } $PinNum = KeyGen(); //First call. $SecondPin = KeyGen($PinNum); //Second Call. echo $PinNum . ' - ' . $SecondPin; //Outputs the same thing twice. Also, I love your signature. You forgot Harp, though. Quote Link to comment https://forums.phpfreaks.com/topic/212455-private-class-functionproperty-repeater-please/#findComment-1107069 Share on other sites More sharing options...
trq Posted September 4, 2010 Share Posted September 4, 2010 I guess what you could do is pass the second pin as a function property and if it's set it just repeats whatever you passed previously. Because this is a method within an object and object have state, it would be easy enough to use a variables to remember how many times this method has been called and what it returned previously. Like I said though, I think its a bad decision. Quote Link to comment https://forums.phpfreaks.com/topic/212455-private-class-functionproperty-repeater-please/#findComment-1107072 Share on other sites More sharing options...
rwwd Posted September 4, 2010 Author Share Posted September 4, 2010 Hi there Thorpe, Could you elaborate a little bit please? So far as I am concerned, this function just creates a four (maybe six) digit code to append to the end of some data, I was previously using shuffle() as well as the rand() but just using rand is less intensive on the parser, then that data is used to form a 'secure' cookie for when a user logs in, and as there are two separate calls to this function I need the third to be the start of the next set; so in essence two sets of two... I'm seriously considering rewriting the way as I do this because I seem to have wandered into a dead end; but the optimist in me says "carry on, the solution is just ahead". @pornophobic Cheers for the input, I can see the logic there, but the calls to the function should be separate calls not calls to itself, though I do like that Idea. As for the signature, I saw something similar a while ago but using food, so my next natural thought was beer! Cheers, Rw Quote Link to comment https://forums.phpfreaks.com/topic/212455-private-class-functionproperty-repeater-please/#findComment-1107144 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.