Page 1 of 1

returning variable from a function

Posted: Sat Apr 23, 2005 8:43 pm
by SFADuncan
This works fine. It sends $title off to another function which displays $title the way I need.

Code: Select all

function extract_title1($published_page)
{
  if (is_array($published_page))
  {
  	foreach ($published_page as $row)
  	{
	$title = $row['title1'];
  	}
  }
  display_hm_title($title);
}
But what I really want to do is return $title somewhere else before sending it to another function. ie I want to return $title, but I can't. I've tried several derivations of the following but it's failing to return $title every time:

Code: Select all

function extract_title1($published_page)
{
  if (is_array($published_page))
  {
  	foreach ($published_page as $row)
  	{
	$title = $row['title1'];
  	}
  }
  return $title;
}
Any ideas?

Simon

Jcart | Please review :arrow: Posting Code in the Forums

Posted: Sat Apr 23, 2005 8:57 pm
by John Cartwright
In your function(s) you should look at making title an array or atleast one long string and passing the array through the return. The way you have it setup is you repeatedly overwrite $title and return the last instance of it.

Posted: Sun Apr 24, 2005 2:54 am
by SFADuncan
I'm convinced you are right, but I'm not sure how to do it... I've tried the following but it doesn't work.

Code: Select all

function extract_title1($published_page)
{
  if (is_array($published_page))
  {
   $title = array();
   foreach ($published_page as $row)
   {
   $title[] = $row['title1'];
   }
  }
  return $title;
}

I'm calling the function like this:

Code: Select all

extract_title1($published_page);

Posted: Sun Apr 24, 2005 3:10 am
by thegreatone2176
i dont really see the point of what your code is doing but i dont think you can do $row['title1'] like with mysql i think you should try $row[0] or w/e element it is you are trying to extract.