doubledee Posted June 26, 2012 Share Posted June 26, 2012 What is the best/proper way to end a branch within a Function if I want the Function to cease running? In a normal PHP script I would just have... exit(); ...but since my Function is inside a PHP file with several other functions, I am thinking that is not what I want. It is late and my brain is tired, but below is the logic I am trying to turn into a Function... On load "profile.php"... if $sessMemberID == $memberID{ // Visitor is Self. exit <==== What should I do here? }elseif is_null($sessMemberID){ // Visitor is Anonymous. $visitorID = 0 }else // Visitor is Someone Else $visitorID = $sessMemberID } INSERT $visitorID, $visitorIP, $visitedOn Maybe if I write my code/logic in a different manner, it would solve things?! Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264780-can-you-exit-inside-a-function/ Share on other sites More sharing options...
kicken Posted June 26, 2012 Share Posted June 26, 2012 It depends on what you want to happen. If you want to kill the script then use exit. If you just want to leave the current function but keep the script going, use return Quote Link to comment https://forums.phpfreaks.com/topic/264780-can-you-exit-inside-a-function/#findComment-1356995 Share on other sites More sharing options...
doubledee Posted June 26, 2012 Author Share Posted June 26, 2012 It depends on what you want to happen. If you want to kill the script then use exit. If you just want to leave the current function but keep the script going, use return See my pseudo-code above... if $sessMemberID == $memberID{ // Visitor is Self. exit <==== What should I do here? If the Visitor is ME, then I don't want my code to go to the INSERT. (The code above is annoying the way the logic flows...) Follow me? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264780-can-you-exit-inside-a-function/#findComment-1356998 Share on other sites More sharing options...
doubledee Posted June 26, 2012 Author Share Posted June 26, 2012 Is it okay to write a Function that does something, but doesn't necessarily return anything?? Here is what I have so far for my "function"... function logVisitor($dbc, $sessMemberID, $memberID){ // Check for Session Data. if (empty($sessMemberID)){ // Session does Not Exist. // Anonymous Visitor. $visitorID = 0; }else{ // Session Exists. if ($sessMemberID == $memberID){ // Visitor is Self. $visitorID = -999; }else{ // Visitor is Another Person. $visitorID = $sessMemberID; } } // ************************** // Attempt to Log Visitor. * // ************************** if ($visitorID >= 0){ // Build query. $q1 = "INSERT INTO visitor_log(visitor_id, ip, hostname, created_on) VALUES(?, ?, ?, NOW())"; // Prepare statement. $stmt1 = mysqli_prepare($dbc, $q1); // Bind variables to query. mysqli_stmt_bind_param($stmt1, 'iss', $visitorID, $ip, $hostname); // Execute query. mysqli_stmt_execute($stmt1); // Verify Insert. if (mysqli_stmt_affected_rows($stmt1)==1){ // Insert Succeeded. $_SESSION['resultsCode'] = 'VISITOR_NEW_VISITOR_LOGGED_xxxx'; }else{ // Insert Failed. $_SESSION['resultsCode'] = 'VISITOR_NEW_VISITOR_NOT_LOGGED_xxxx'; }// End of VERIFY INSERT. // Set Error Source. $_SESSION['errorPage'] = $_SERVER['SCRIPT_NAME']; // Redirect to Display Outcome. header("Location: " . BASE_URL . "/account/results.php"); // End script. exit(); }else{ // Do nothing... }//End of ATTEMPT TO LOG VISITOR // return $commentEditable; ?????????????? }//End of logVisitor Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264780-can-you-exit-inside-a-function/#findComment-1356999 Share on other sites More sharing options...
KevinM1 Posted June 26, 2012 Share Posted June 26, 2012 Yes, it's okay to have functions that don't return a value. In statically typed languages, they're often denoted with the 'void' keyword. Quote Link to comment https://forums.phpfreaks.com/topic/264780-can-you-exit-inside-a-function/#findComment-1357050 Share on other sites More sharing options...
cyberRobot Posted June 26, 2012 Share Posted June 26, 2012 For more information on the return statement, the following might help: http://php.net/manual/en/function.return.php Quote Link to comment https://forums.phpfreaks.com/topic/264780-can-you-exit-inside-a-function/#findComment-1357059 Share on other sites More sharing options...
doubledee Posted June 26, 2012 Author Share Posted June 26, 2012 Yes, it's okay to have functions that don't return a value. In statically typed languages, they're often denoted with the 'void' keyword. When I first started programming, we used to call "functions" that didn't return a value a "Procedure"... Do you think the code I posted above would be better suited in a PHP Include file, or is leaving it in a PHP Function okay? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264780-can-you-exit-inside-a-function/#findComment-1357230 Share on other sites More sharing options...
KevinM1 Posted June 27, 2012 Share Posted June 27, 2012 You should do whatever you feel best balances ease of use and readability. Quote Link to comment https://forums.phpfreaks.com/topic/264780-can-you-exit-inside-a-function/#findComment-1357301 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.