AbydosGater Posted September 21, 2006 Share Posted September 21, 2006 Hi ok well first the background, the function is within my config file which is included within the file i am having problems with, and the page i have another function already running to connect to the database,Now here is the code to my function...[code]function pagenav(){ if (isset($_GET['id'])) { $result = mysql_query("SELECT * FROM ben_pages WHERE id='$_GET[id]'") or die(mysql_error()); $row = mysql_fetch_array( $result ); $pagetitle = $row['title']; $pagecontent = $row['page']; //echo $pagetitle ; //echo "<br />"; //echo $pagecontent ; } else { $result = mysql_query("SELECT * FROM ben_pages WHERE id='index'") or die(mysql_error()); $row = mysql_fetch_array( $result ); $pagetitle = $row['title']; $pagecontent = $row['page']; global $pagetitle, $pagecontent; //echo $pagetitle; //echo "<br />"; //echo $pagecontent; };};[/code]Now if i uncomment the echos there, I do get the variables to display!, But i have that function running on another page, but i have the page displaying $pagetitle , $pagecontent at another point outside the function, but it isnt displaying, how come? i have them set to global? anyone any ideas?, any help atall would be great?Thanks Link to comment https://forums.phpfreaks.com/topic/21543-vars-within-function-made-global-but-not-displaying/ Share on other sites More sharing options...
shoz Posted September 21, 2006 Share Posted September 21, 2006 Declare the variables global before assigning them values.[code]global $pagetitle, $pagecontent;[/code]http://www.php.net/manual/en/language.variables.scope.phpI'd recommend you simply return an array with the relevant information rather than setting them global.[code]function pageNav(){ $pageData['title'] = $row[..] $pageData['content'] = $row[...]; return $pageData;}$pageData = pageNav();echo $pageData['title']."<br />\n";echo $pageData['content']."<br />\n";[/code] Link to comment https://forums.phpfreaks.com/topic/21543-vars-within-function-made-global-but-not-displaying/#findComment-96153 Share on other sites More sharing options...
AbydosGater Posted September 21, 2006 Author Share Posted September 21, 2006 ehh, im not that good and dont really understand that :P Link to comment https://forums.phpfreaks.com/topic/21543-vars-within-function-made-global-but-not-displaying/#findComment-96155 Share on other sites More sharing options...
shoz Posted September 21, 2006 Share Posted September 21, 2006 [quote author=AbydosGater link=topic=108934.msg438807#msg438807 date=1158857110]ehh, im not that good and dont really understand that :P[/quote][code]function add($a, $b){ $total = $a +$b; return $total; //"return $a + $b" would also achieve the same result.}$aVarl = add(5, 1);print $aVar //Prints "6"[/code]http://www.php.net/manual/en/functions.returning-values.php Link to comment https://forums.phpfreaks.com/topic/21543-vars-within-function-made-global-but-not-displaying/#findComment-96159 Share on other sites More sharing options...
AbydosGater Posted September 21, 2006 Author Share Posted September 21, 2006 Ah ok yeah i get it now! Thanks its all working now! Link to comment https://forums.phpfreaks.com/topic/21543-vars-within-function-made-global-but-not-displaying/#findComment-96163 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.