Jump to content

[SOLVED] functions :: Undefined variable


Recommended Posts

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 ;D

 

~ Chocopi

Link to comment
https://forums.phpfreaks.com/topic/70764-solved-functions-undefined-variable/
Share on other sites

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

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

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;

Thanks Wildteen ;D

 

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 :P

 

But thanks Rarebit + Wildteens !!!

 

~ Chocopi

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.