0

I have a functions.php file with the following content.

function maxSavePerHour($ip, $maxSave = 15){
//DB connection and query. Working fine.
$totalRows = mysql_num_rows($res);
    if($totalRows <=$maxSave){
        $safe=true;
    }else{
        $safe=false;
    }

if($safe){
    echo "OK";
}else{
    echo "Not OK";
}
}

I call that function from another page. And I use the function as following.

maxSavePerHour($ip, 5);

If I change the value the function echoes text as it should. The problem is the $safe variable. If I echo $safe i get nothing.

Any clues? The reason I need $safe to be true or false is because I use it in a if statement later.

I put global $safe;` and now its working. Thanks Nile.

11
  • 2
    i am unable to see any function definition and what do you mean by: "i call that function from another page"? Commented Aug 7, 2012 at 5:59
  • you may need to put global $safe at the beginning of your function, but I couldn't know without seeing the whole thing. Commented Aug 7, 2012 at 6:00
  • 3
    Very unclear question, please show more code context and where you output the $safe variable. Spontaneous guess: variable scope! Commented Aug 7, 2012 at 6:00
  • What if you var_dump($safe) ? Commented Aug 7, 2012 at 6:00
  • 2
    @Nile No, he does not need to put global anywhere. Commented Aug 7, 2012 at 6:01

4 Answers 4

1

If you echo $safe, it is echoing nothing because $safe is a boolean value (and false will not echo a value, whereas true will echo 1). I would return the value of $safe and then change the calling code to handle this. E.g.:

function maxSavesPerHour($ip, $maxSave) {
  $totalRows = mysql_num_rows($res);
  return ( $totalRows <= $maxSave ? TRUE : FALSE );
}

Then call it from your code like this:

if ( maxSavesPerHour($ip, 5) ) {
  //do something when true
} else {
 //do something when false
}
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think the boolean value is a problem, but this is the only answer with sensible code. +1
1
function maxSavePerHour($ip, $maxSave = 15){
//DB connection and query. Working fine.
$totalRows = mysql_num_rows($res);
    if($totalRows <=$maxSave){
        $safe=true;
    }else{
        $safe=false;
    }

    if($safe){
        echo "OK";
    }else{
        echo "Not OK";
    }
    return $safe;
}
$safe = maxSavePerHour($ip, 5);
echo $safe;

1 Comment

boolean are not really well showed with echo better use var_dump
1

You should be able to simply return $safe and handle it from the other file. Or better yet, implement the following:

function maxSavePerHour($ip, $maxSave = 15){
//DB connection and query. Working fine.
$totalRows = mysql_num_rows($res);
    if($totalRows <=$maxSave){
        return true;
    }else{
        return false;
    }
}

You do not actually need a $safe variable. Marking your $safe variable as global is a very bad idea and should hardly ever be done.

1 Comment

From where you call the method you can do something of this sort: if(maxSavePerHour('valuesfor$ip', 15)){//true} else {//false} or $ok = maxSavePerHour('valuesfor$ip', 15); if($ok){//do something} else {//do someething else}
0
function maxSavePerHour($ip, $maxSave = 15){ 
    //DB connection and query. Working fine. 
    $totalRows = mysql_num_rows($res); 
    $safe = false;
    if($totalRows <=$maxSave){ 
         $safe=true; 
    }else{ 
         $safe=false; 
    } 

    if($safe){ 
        echo "OK"; 
    }else{ 
        echo "Not OK"; 
    } 
} 

safe only exsists in the if not outside it

1 Comment

@mithunsatheesh I was on a mobile phone so I couldn't format it, thanks :)

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.