xlxprophetxlx Posted May 20, 2010 Share Posted May 20, 2010 I can't seem to echo this out of a function: mt_srand ((double) microtime() * 1000000); $useridstring = md5(uniqid(mt_rand())); $userfinal = ""; function useridfun(){ if($userfinal == ""){ echo $useridstring; }elseif($userfinal == $userid){ echo $userfinal; } } useridfun() This is pretty much driving me nuts. I had it working earlier but now it doesn't seem to want to work. If I print or echo $useridstring by itself it works but not within the function. It got to be something I'm just over seeing. Thanks! Link to comment https://forums.phpfreaks.com/topic/202438-does-any-one-see-anything-wrong-with-this-random-id/ Share on other sites More sharing options...
-Karl- Posted May 20, 2010 Share Posted May 20, 2010 Where's userid defined? Link to comment https://forums.phpfreaks.com/topic/202438-does-any-one-see-anything-wrong-with-this-random-id/#findComment-1061401 Share on other sites More sharing options...
xlxprophetxlx Posted May 21, 2010 Author Share Posted May 21, 2010 Where's userid defined? userid is defined further up in my coding but in this example it shouldn't matter since $userfinal == "" is true as shown. and it's not outputting echo $useridstring; for some reason. Link to comment https://forums.phpfreaks.com/topic/202438-does-any-one-see-anything-wrong-with-this-random-id/#findComment-1061627 Share on other sites More sharing options...
kenrbnsn Posted May 21, 2010 Share Posted May 21, 2010 You have a variable scope problem. The variable $userid inside the function has no relation to any similarly named function outside the function. You need to pass the value into the function if you want to use it. Same with the variable $userfinal. Try something like this: <?php $userfinal = ""; function useridfun($userfinal, $userid) { mt_srand ((double) microtime() * 1000000); $useridstring = md5(uniqid(mt_rand())); if($userfinal == ""){ $tmp = $useridstring; }elseif($userfinal == $userid){ $tmp = $userfinal; } return($tmp); } $userfinal = useridfun($userfinal, $userid); echo $userfinal; ?> Ken Link to comment https://forums.phpfreaks.com/topic/202438-does-any-one-see-anything-wrong-with-this-random-id/#findComment-1061629 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.