Jump to content

[SOLVED] User Defined Function


shamuraq

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/166416-solved-user-defined-function/
Share on other sites

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>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.