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
https://forums.phpfreaks.com/topic/170872-solved-function-help-anyone/
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

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.