Jump to content

Recommended Posts

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?!  :shrug:

 

Thanks,

 

 

Debbie

 

Link to comment
https://forums.phpfreaks.com/topic/264780-can-you-exit-inside-a-function/
Share on other sites

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

 

Is it okay to write a Function that does something, but doesn't necessarily return anything??  :confused:

 

 

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

 

 

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.