Jump to content

Globalizing variables inside of a function


Goldeneye

Recommended Posts

I've got a few variables inside of a function (which I put at the top of all my scripts) that I need to use for queries and $_GET parameters. How would I make these variables accessible outside of the function?

 

<?php
function sHeader($itemlimit=0, $reqDB=1){
	session_start();
	if($reqDB=1){
		connectToMySQLdb();
		$GLOBALS['board'] = mysql_real_escape_string(intval($_GET['board']));
		$topic = mysql_real_escape_string(intval($_GET['topic']));
		$message = mysql_real_escape_string(intval($_GET['message']));
		$page = mysql_real_escape_string(intval($_GET['page']));
		// Now to Globalize the variables
		$board = $GLOBALS['board'];
		global $topic;
	}
}

if($_GET['page'] == $page){
	// Foobar 
}
?>

 

When I try referencing those globalized variables, I get errors in my script. I know using GLOBALS is not recommended, but I don't know of any alternatives. (If anyone could give some, that'd be appreciated).

Well wildteen88, I have tried 'return' but reading up on it, I came across this little snippet: "If called from within a function, the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call."

 

<?php
function sHeader($itemlimit=0, $reqDB=1){
	session_start();
	if($reqDB=1){
		connectToMySQLdb();
		return $board = mysql_real_escape_string(intval($_GET['board']));
		return $topic = mysql_real_escape_string(intval($_GET['topic']));
		return $message = mysql_real_escape_string(intval($_GET['message']));
		return $page = mysql_real_escape_string(intval($_GET['page']));
	}
}

if($_GET['page'] == $page){
	$query = mysql_query("SELECT * FROM `table` WHERE `param`=$page") or die(mysql_error());
}
?>

I did try that, but it didn't seem to work.

 

I also tried doing:

<?php
		$board = mysql_real_escape_string(intval($_GET['board'])); return $board;
		$topic = mysql_real_escape_string(intval($_GET['topic'])); return $topic
		$message = mysql_real_escape_string(intval($_GET['message'])); return $message
		$page = mysql_real_escape_string(intval($_GET['page'])); return $page
?>

 

Also, I could simply use $_GET, but I need to be able to access the variable name. Aside from that, this function comes from a file that I include()'d at the top of my script.

return an array instead of individual vars.

 

// example function
function something () {
$blah['board'] = mysql_real_escape_string(intval($_GET['board']));
$blah['topic'] = mysql_real_escape_string(intval($_GET['topic']));
$blah['message'] = mysql_real_escape_string(intval($_GET['message']));
$blah['page'] = mysql_real_escape_string(intval($_GET['page']));

return $blah;
}

// example call to function
$vars = something();

// example use of individual var
echo $vars['board'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.