Goldeneye Posted July 26, 2008 Share Posted July 26, 2008 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). Quote Link to comment https://forums.phpfreaks.com/topic/116750-globalizing-variables-inside-of-a-function/ Share on other sites More sharing options...
wildteen88 Posted July 26, 2008 Share Posted July 26, 2008 $_GET is a global variable, so why not use that. Or use return instead. Quote Link to comment https://forums.phpfreaks.com/topic/116750-globalizing-variables-inside-of-a-function/#findComment-600384 Share on other sites More sharing options...
discomatt Posted July 26, 2008 Share Posted July 26, 2008 Or pass by reference... http://php.net/manual/en/language.references.pass.php Quote Link to comment https://forums.phpfreaks.com/topic/116750-globalizing-variables-inside-of-a-function/#findComment-600386 Share on other sites More sharing options...
wildteen88 Posted July 26, 2008 Share Posted July 26, 2008 Or pass by reference... http://php.net/manual/en/language.references.pass.php The variables Goldeneye wants to be global are defined in the function, so pass by reference wont work. Quote Link to comment https://forums.phpfreaks.com/topic/116750-globalizing-variables-inside-of-a-function/#findComment-600391 Share on other sites More sharing options...
Goldeneye Posted July 26, 2008 Author Share Posted July 26, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/116750-globalizing-variables-inside-of-a-function/#findComment-600438 Share on other sites More sharing options...
.josh Posted July 26, 2008 Share Posted July 26, 2008 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']; Quote Link to comment https://forums.phpfreaks.com/topic/116750-globalizing-variables-inside-of-a-function/#findComment-600442 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.