shamuraq Posted July 18, 2009 Share Posted July 18, 2009 Hi all, I'm trying to create my own pre-defined function and i'm stuck... I must apologise before hand if my query appears stupid. I still am expanding my understanding in php so any help is truly appreciated. <? function timex() { $h = rand(1,12); $m = rand(1,60); $s = rand(1,60); } //First timing $timex1 = timex($h,$m,$s); echo $timex1.'<br>'; //Second timing $timex2 = timex($h,$m,$s); echo $timex2.'<br>'; //Third timing $timex3 = timex($h,$m,$s); echo $timex3.'<br>'; ?> I can't seem to get it to work and i know its purely syntax error. I tried looking around but i cannot find one with my approach. I'm trying to recycle the function timex() everytime a variable calls for it. Thanx in advance... Quote Link to comment https://forums.phpfreaks.com/topic/166416-solved-user-defined-function/ Share on other sites More sharing options...
MadTechie Posted July 18, 2009 Share Posted July 18, 2009 you need to return something from the function.. ie function timex() { $h = rand(1,12); $m = rand(1,60); $s = rand(1,60); return "$h:$m:$s"; //return a string of $h:$m:$s } //also you are don't need to pass anything as you don't use them //so to call the function use //First timing $timex1 = timex(); echo $timex1.'<br>'; EDIT: okay another example function timex2($h1) { $h = rand($h1,12); //random from $h1 to 12 $m = rand(1,60); $s = rand(1,60); return "$h:$m:$s"; //return a string of $h:$m:$s } $timex1 = timex2(5); //passes 5 to $h1 (so gernerates a random hour from 5 to 12) echo $timex1.'<br>'; Quote Link to comment https://forums.phpfreaks.com/topic/166416-solved-user-defined-function/#findComment-877572 Share on other sites More sharing options...
shamuraq Posted July 18, 2009 Author Share Posted July 18, 2009 Thanx mate.... appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/166416-solved-user-defined-function/#findComment-877671 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.