Page 1 of 1

function return true vs return nothing

Posted: Fri Mar 11, 2011 6:23 pm
by meee
Is it a good practice that I put in functions that don't return anything return true statement or can be without? I have two example. Is it ok to leave it like that?

Code: Select all

function checkAdminLogin ()
{
    if ( empty($_SESSION['administrator']) || $_SESSION['administrator']!=ADMINISTRATOR_PASSWORD) { 
        header ("Location: /administration/login.php");
    }
}

function updateCounter ()
{
      global $db;
      $db->query("UPDATE users SET counter=counter+1"); 
}

Re: function return true vs return nothing

Posted: Fri Mar 11, 2011 8:11 pm
by mecha_godzilla
You would normally return a value back to the calling script to confirm that the function() completed successfully (or otherwise).

Just looking at your first example, when you use header() you would normally use exit() afterwards to stop the rest of the script executing so you don't need to return any kind of status back to the calling script.

Similarly - with your second example - what happens if your database isn't available for some reason? If your DB class handles this situation itself then you don't need to return any values. This kind of failure becomes critical if you're saving information to the database or sending an email and the operation can't be completed - in these situations it's important that the user knows the operation has failed.

HTH,

Mecha Godzilla