Jump to content

[SOLVED] Function help anyone


shamuraq

Recommended Posts

Hi there,

 

I'm trying to do a function to get the values of $hour_1,$min_1,$hour_name and $min_name.

Can anyone help me see what went wrong?

<? php
function min2hrmin($hour_1,$min_1,$hour_name,$min_name){

$min = rand(60,999);
//echo $min.'<br>';
$hour = $min / 60;

$hour_1 = floor($min / 60);
	  //Sorting Singular and plural form
	  if($hour_1 < 1){
		  $hour_name = 'hour';
	  }
	  else{
		  $hour_name = 'hours';
	  }

$min_1 = $min % 60;
	  //Sorting Singular and plural form
	  if($min_1 < 1){
		  $min_name = 'minute';
	  }
	  else{
		  $min_name = 'minutes';
	  }
return ($hour_1,$min_1,$hour_name,$min_name);	  
} 
echo $hour.'<br>';
echo $min.'<br>';
echo $hour_1.'<br>';
echo $min_1.'<br>';
echo $hour_name.'<br>';
echo $min_name.'<br>';
?>

Link to comment
Share on other sites

However return does not return variables, it returns the value of a variable.

 

Unless you wrap it inside an array:

 

return array($hour_1,$min_1,$hour_name,$min_name);

 

acess using:

 

list($hour_1, $min_1, $hour_name, $min_name) = min2hrmin(..);//assuming you want to overwrite $hour_1, .. use different variable names otherwise

 

Don't calculate $hour twice:

 

$hour = $min / 60;
$hour_1 = floor($hour/*not $min / 60*/);

 

if($hour_1 < 1)

 

Always returns false as $min >= 60 && $min <= 999, thus $hour_1 is always >= 1

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.