Page 1 of 1

problems calling a variable back from a function?

Posted: Fri Jan 09, 2004 10:17 am
by Pash
Sorry but im really new can someone help :oops: .

Im trying to return a value from a function but it just displays nothing. Below is my code am I making some mistakes with the variables? The variable value for action is being passed on from a previous page through the url.

Code: Select all

<?php
function loadaction ()

&#123;

if ($action == "loadprofile")

&#123;

$Headergfx = "bling.gif";

&#125;


return $Headergfx;

&#125;

loadaction($action);

echo $Headergfx;

?>
it just doesnt display a thing, any help really apprechiated!

Posted: Fri Jan 09, 2004 10:21 am
by JayBird
perhaps like this

Code: Select all

<?php 
function loadaction ($action) 
{ 
	if ($action == "loadprofile") 
	{ 
		$Headergfx = "bling.gif"; 
	} 

	return $Headergfx; 
} 

echo loadaction($action); 

?>
Mark

Posted: Fri Jan 09, 2004 10:21 am
by malcolmboston
<--just realised he also needs to learn functions

Posted: Fri Jan 09, 2004 10:31 am
by Pash
that is great thx but what do I do to make that value that is being returned a variable itself so i can use it later on?

Posted: Fri Jan 09, 2004 10:34 am
by JayBird
like this

Code: Select all

<?php 
function loadaction ($action) 
{ 
   if ($action == "loadprofile") 
   { 
      $Headergfx = "bling.gif"; 
   } 

   return $Headergfx; 
} 

$newVariable = loadaction($action); 

?>

Mark

Posted: Fri Jan 09, 2004 10:38 am
by Pash
thx very much, just figured it out as you posted it but thx again for your help.

Posted: Fri Jan 09, 2004 12:04 pm
by Gen-ik
Just as a quick note for future function type use you might want to try the following... depending on how large you intend to make your function().

Code: Select all

<?php 

function loadaction($action) 
{
   if($action == "loadprofile") return "bling.gif";
   if($action == "loadprofileB") return "blingB.gif";
   if($action == "loadprofileC") return "blingC.gif";

   return "default.gif" // Returns default.gif if no previous returns are sent.
}

$newVariable = loadaction($action);

?>
And there is always the [php_man]switch[/php_man] option ;)

Posted: Fri Jan 09, 2004 2:20 pm
by Bennettman
To call more than one function, return an array and use list to assign them:

Code: Select all

<?

function loadaction($action) {
   if ($action == "whatever") {
      $variable1 = "meh";
      $variable2 = "moo";
   }

   return array($variable1, $variable2);
}

list($newvar1, $newvar2) = loadaction($action);

?>