2

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

3
  • is the time coming from the db Unix timestamp? Could just be a problem with different timestamps. Commented May 6, 2011 at 18:43
  • You have many earlier questions. Please revisit them and accept answers. You'll get more help here if you do so. Commented May 6, 2011 at 18:47
  • lol, you guys are really fast thank you for your help Commented May 6, 2011 at 18:59

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much i did it like that $result = checkIpn(); if($result[0]==false) $err[]='you need to wait '.$result[1].' minutes before you send another request';
2

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.

Comments

1

You could return an array containing as many things as you like.

Comments

0

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.

3 Comments

An array is exactly one return value and an array with fixed size is call "tupel" ("n-tupel") in other languages, as well as even in math. Dont see any bad style, just the need to document it proper.
I agree with that and agree with your semantic definition that it is technically one return value, but I don't like the idea of returning an array with a boolean and an integer. It makes the code confusing.
I also think this is correct solution (returning array) and I think in Python it is called list, but seeing this code it is possible to return one value not being a tuple and not being a list / array, but boolean / integer. Just look at my solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.