Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

exit a function 2

Status
Not open for further replies.

y2k1981

Programmer
Aug 2, 2002
773
IE
I want to test if a certain condition is true, then if it is, exit the function, but continue running the rest of the code on the page. I've tried exit and end, but neither of them do it. They either exit everything after the exit line, or do nothing at all.

 
Code:
<?php

function something(){
   do some stuff;
   if ( condition ) return;
   do some more stuff;
}

?>

HTML
HTML
HTML
HTML

<?php something(); ?>

MORE HTML
MORE HTML
MORE HTML
MORE HTML
MORE HTML

Or are you using the term "function" imprecisely?
 
If you post a bit of code it would be easier to advise.

exit stops execution of the script.
return ends execution of the function and returns to the place where the function was called. If a parameter is supplied it will be handed back. The result can be captured by assigning the return value to a variable.
 
Cheers, return is exactly what I'm looking for. I've just tested it out now and it works. Thanks again
 
Generally speaking, it is better to have only one exit point for a function. It makes the function easier to debug.

I would recommend something more like:

Code:
<?php
function something()
{
   //do something;
   if (! condition)
   {
      //do some more stuff;
   }
   return;
}

print 'HTML
HTML
HTML
HTML';

php something();

print 'MORE HTML
MORE HTML
MORE HTML
MORE HTML
MORE HTML';
?>

If the function is not going to return a value, then the return() invocation is not necessary.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Maybe I should post some code then, so here goes:

functions.php:
Code:
<?PHP
function checkAuthorized($authorizedGroup)
}
$userid = $_SESSION['userid'];
$get_user_groups = mysql_query("SELECT * FROM usersingroups WHERE userid = '$userid'");

while($user_groups = mysql_fetch_array($get_user_groups))
   {
     if($user_groups['groupid']==$authorizedGroup)
       {
          return;
        }
   }
Header("Location: [URL unfurl="true"]http://myserver/unauthorized.php");[/URL]

}
?>

Then on the pages I want this on, I just put the following at the top:
Code:
include functions.php;
checkAuthorized("00");

That way, if the user is in the group, it will exit the function and continue loading the page, if the user is not in the group, it will get to Header... and send the user to the unauthorized page.

If anybody can improve on this, please feel free as I'm only a beginner.
 
I would make better use of MySQL's capabilities in my query, trap more errors, and have only one function exit point.

Code:
<?php
function CheckAuthorized ($authorizedGroup)
{
	$retval = FALSE;
	if (isset ($_SESSION['userid']))
	{
		$get_group_count = mysql_query ("SELECT count(*) as group_count FROM usersingroups WHERE userid = '" . $_SESSION['userid'] . "' and groupid = '" . $authorizedGroup . "'");
		
		if ($get_group_count != FALSE)
		{
			list($matching_group_count) = mysql_fetch_array ($get_user_groups);

			if($matching_group_count != 0)
			{
				$retval = TRUE;
			}
		}
		else
		{
			//provide mechanism for logging/displaying the connect error.
		}
	}
	
	return $retval;
}


if (CheckAuthorized("00") == FALSE)
{
	header("Location: [URL unfurl="true"]http://myserver/unauthorized.php");[/URL]
}
?>


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top