Jump to content

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'];

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.