chocopi Posted September 26, 2007 Share Posted September 26, 2007 I've never really used functions but now I find myself needing to. I understand the basics but I can't seem to get the variable from within the function to echo outside of it. <?php function topic($topic,$cat) { if(empty($_GET['topic'])) { header ("Location: index.php"); } else { $topic = $_GET['topic']; if(!ereg("^([0-9])+$",$topic)) { die("The Topic '{$topic}' You Have Selected Does Not Exist !"); } else { $query = mysql_query("SELECT topic_name FROM fori_topic WHERE topic_id='$topic' AND cat_id='$cat'") or die(mysql_error()); $num_rows = mysql_num_rows($query) or die(mysql_error()); $row = mysql_fetch_assoc($query) or die(mysql_error()); $topic_name = trim(ucwords(strtolower($row['topic_name']))); // This one } if($num_rows == 0) { die("The Topic Does Not Exist Within The Chosen Category !"); } } } ?> so when I call it later on with: <?php require_once("functions/function_topic.php"); topic($topic,$cat); echo $topic_name; ?> I get Undefined variable notice on the line i'm echoing. I know there is a way to do this but I can't figure it out. Many Thanks ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/70764-solved-functions-undefined-variable/ Share on other sites More sharing options...
rarebit Posted September 26, 2007 Share Posted September 26, 2007 Here's an example (you need to return something!) function myfunk() { $s = "hi"; return $s; } echo myfunk(); You can return most things... Quote Link to comment https://forums.phpfreaks.com/topic/70764-solved-functions-undefined-variable/#findComment-355782 Share on other sites More sharing options...
chocopi Posted September 26, 2007 Author Share Posted September 26, 2007 Thats what I thought but it still gives the same error even after adding return $topic_name; within the function ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/70764-solved-functions-undefined-variable/#findComment-355785 Share on other sites More sharing options...
rarebit Posted September 26, 2007 Share Posted September 26, 2007 Who's got the banana? function myfunk() { $s = "hi"; return $s; } $v myfunk(); echo $v; Quote Link to comment https://forums.phpfreaks.com/topic/70764-solved-functions-undefined-variable/#findComment-355790 Share on other sites More sharing options...
chocopi Posted September 26, 2007 Author Share Posted September 26, 2007 i dont understand what you mean Thats gives a parse error when the variable is infront of yhr function ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/70764-solved-functions-undefined-variable/#findComment-355797 Share on other sites More sharing options...
rarebit Posted September 26, 2007 Share Posted September 26, 2007 And so it should, it needs an equals sign! $v = myfunk(); Quote Link to comment https://forums.phpfreaks.com/topic/70764-solved-functions-undefined-variable/#findComment-355800 Share on other sites More sharing options...
chocopi Posted September 26, 2007 Author Share Posted September 26, 2007 Cheers Rarebit, I never thought that would work as you are then storing the function within the variable, but it does so I'll just shut up Thanks ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/70764-solved-functions-undefined-variable/#findComment-355805 Share on other sites More sharing options...
chocopi Posted September 26, 2007 Author Share Posted September 26, 2007 I was wondering, how could I get it to return 2 values You can't return multiple values from a function' date=' but similar results can be obtained by returning a list.[/quote'] Is this the only way to do it, or is there a better option ? ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/70764-solved-functions-undefined-variable/#findComment-355836 Share on other sites More sharing options...
rarebit Posted September 26, 2007 Share Posted September 26, 2007 That's the way: ... return list($x, $y); } with either: list($x, $y) = myfunc(); or $z = myfunc(); Quote Link to comment https://forums.phpfreaks.com/topic/70764-solved-functions-undefined-variable/#findComment-355838 Share on other sites More sharing options...
chocopi Posted September 26, 2007 Author Share Posted September 26, 2007 I get what your doing but I can't get it to return the values <?php $array = array($post,$post_message,$post_subject); return list($post,$post_message,$post_subject) = $array; ?> And I am using it with list($post,$post_message,$post_subject) = action($topic,$cat); ~Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/70764-solved-functions-undefined-variable/#findComment-355846 Share on other sites More sharing options...
rarebit Posted September 26, 2007 Share Posted September 26, 2007 That makes no sense to me? lol For this: return list($post,$post_message,$post_subject) = $array; which I presume is the function returning, you do either or, i.e. return the array you created, or return the list set... Quote Link to comment https://forums.phpfreaks.com/topic/70764-solved-functions-undefined-variable/#findComment-355848 Share on other sites More sharing options...
chocopi Posted September 26, 2007 Author Share Posted September 26, 2007 Well if I do return list($variables); I get a notice saying: Unexpected ; expecting = Thats the only reason i added the = $array; ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/70764-solved-functions-undefined-variable/#findComment-355864 Share on other sites More sharing options...
wildteen88 Posted September 26, 2007 Share Posted September 26, 2007 list can only be used in conjunction with arrays. if you want to return multiple variables from your function you're best of of storing all the variables to be returned into an array, then use list out side of your function: eg: function myfunc() { // vars to be returned $var1 = 'foo'; $var2 = 'bar'; // set up an array to store all variables to be returned. $return = array($var1, $var2); // return the array of variables return $return; // ALTERNATIVELY you could do: // return array($var1, $var2); // instead. } // gets vars out of array of variables return from myfunc list(var1, $var2) = myfunc(); // use variables echo $var1; echo $var2; Quote Link to comment https://forums.phpfreaks.com/topic/70764-solved-functions-undefined-variable/#findComment-355877 Share on other sites More sharing options...
chocopi Posted September 27, 2007 Author Share Posted September 27, 2007 Thanks Wildteen Thats sorted it. I couldn't seem to get it to work and was pulling my hair out but it was because I was over-ridding variables for no reason But thanks Rarebit + Wildteens !!! ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/70764-solved-functions-undefined-variable/#findComment-356645 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.