problems calling a variable back from a function?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Pash
Forum Newbie
Posts: 7
Joined: Mon Jan 05, 2004 4:30 am

problems calling a variable back from a function?

Post 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!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

perhaps like this

Code: Select all

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

	return $Headergfx; 
} 

echo loadaction($action); 

?>
Mark
Last edited by JayBird on Fri Jan 09, 2004 10:25 am, edited 2 times in total.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

<--just realised he also needs to learn functions
Pash
Forum Newbie
Posts: 7
Joined: Mon Jan 05, 2004 4:30 am

Post 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?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

like this

Code: Select all

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

   return $Headergfx; 
} 

$newVariable = loadaction($action); 

?>

Mark
Pash
Forum Newbie
Posts: 7
Joined: Mon Jan 05, 2004 4:30 am

Post by Pash »

thx very much, just figured it out as you posted it but thx again for your help.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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 ;)
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post 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);

?>
Post Reply