cant return 2 variables in php by calling return function?
1st i have to say that i am not php professional and this is my 1st time to use return().
so here is the code. i need to return false and the number of minutes left from the function.
if(!checkIpn())
$err[]='you need to wait'.$nresting.'minutes before you send another request';
function checkIpn()
{
$useripe = 0;
if ( isset($_SERVER["REMOTE_ADDR"]) ) { $useripe = $_SERVER["REMOTE_ADDR"] ; }
else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ) { $useripe = $_SERVER["HTTP_X_FORWARDED_FOR"] ; }
else if ( isset($_SERVER["HTTP_CLIENT_IP"]) ) { $useripe = $_SERVER["HTTP_CLIENT_IP"] ; }
$query = "SELECT * FROM table WHERE ip = '$useripe' AND status = 'pending' ORDER BY id ASC";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
if ( $num_rows > 0 )
{
$str_today = date("Y-m-d H:i:s");
$i=1;
while($row = mysql_fetch_assoc($result))
{
$str_start = $row['date'];
$str_start = strtotime($str_start);
$str_end = strtotime($str_today);
$nseconds = $str_end - $str_start;
开发者_C百科 $nminutes = round($nseconds / 60);
if ( $nminutes > 120 )
{ return true; }
else {
$nresting = 120 - $nminutes;
return false; }
$i++;
}
}
else { return true; }
based on Tadeck answer below.i did it like this:
$result = checkIpn();
if($result[0]==false)
$err[]='you need to wait '.$result[1].' minutes before you send another request';
thank you Tadeck.
If you want to return two different variables using single call, you can use something like:
return array(true, 0);
or
return array(true);
in first case (when returning success) and
return array(false, $minutes);
in second case (returning failure).
Then you can check it that way:
$result = checkIpn();
if ($result[0]) {
echo 'Success.';
} else {
echo 'Failure. Minutes to wait: '.$result[1];
}
I hope this helps.
EDIT:
If I understand you correctly, you return true
or the number of minutes > 0
(larger than zero). Thus you can use such return in case of success:
return true;
and this in case of failure:
return $minutes;
Then you will be able to use it in the code in the following way:
$result = checkIpn();
if ($result === true) {
echo 'Success';
} else {
echo 'Failure. Minutes to wait: '.$result;
}
And I would like to advise you to properly document such function, so that no one is surprised when it returns integer instead of boolean ;)
You can return an array instead of booleans, which allows you to return more than one value:
return array('success'=>FALSE, 'nminutes'=>$nminutes);
But as an alternative, you can just return NULL if the function is successful and return the number of minutes if not. Then you don't need TRUE/FALSE
at all.
if ($nminutes > 120)
{
return NULL;
}
else
{
return $nminutes;
}
Check the success like so:
if (checkIpn() !== NULL) // you have minutes left
else // no minutes left - your TRUE case.
You could return an array containing as many things as you like.
In php you can return an array and use the list()
function to unpack the values.
function myfn() {
return array(true, 120);
}
...
list($retval, $minutes) = myfn();
This is generally pretty bad style though and I would recommend finding a way to accomplish what you need with 1 return value.
精彩评论