Question about function returns

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TomTraubert
Forum Newbie
Posts: 2
Joined: Wed Jul 18, 2007 8:26 pm

Question about function returns

Post by TomTraubert »

If I have the following code:

Code: Select all

function myFunction() {
    $myDbObject = new DbObject;
    
    $myDbObject->dbOpen();
    $records = $myDbOject->getRecords();

    if (mysql_num_rows($records) > 0) {
        echo ("success!");
        return TRUE;
    }
							
    $myDbObject->dbClose();
}
Does the dbClose method of myObject get fired before myFunction sends control back to the calling routine? Or does return TRUE; happen after the entire function is handled?

Advice is appreciated!

-- Tom
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

As soon as a return is executed then the function ends, no more code is executed afterwards. In this case if your IF statement evaluated to true, then the function would return true and dbClose() would never be executed.
TomTraubert
Forum Newbie
Posts: 2
Joined: Wed Jul 18, 2007 8:26 pm

Post by TomTraubert »

Thanks Begby. Guess I'll add a few more lines of code to that function, then. :)
Post Reply