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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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