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
Pash
Forum Newbie
Posts: 7 Joined: Mon Jan 05, 2004 4:30 am
Post
by Pash » Fri Jan 09, 2004 10:17 am
Sorry but im really new can someone help
.
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 ()
{
if ($action == "loadprofile")
{
$Headergfx = "bling.gif";
}
return $Headergfx;
}
loadaction($action);
echo $Headergfx;
?>
it just doesnt display a thing, any help really apprechiated!
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Fri Jan 09, 2004 10:21 am
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 » Fri Jan 09, 2004 10:21 am
<--just realised he also needs to learn functions
Pash
Forum Newbie
Posts: 7 Joined: Mon Jan 05, 2004 4:30 am
Post
by Pash » Fri Jan 09, 2004 10:31 am
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?
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Fri Jan 09, 2004 10:34 am
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 » Fri Jan 09, 2004 10:38 am
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 » Fri Jan 09, 2004 12:04 pm
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 » Fri Jan 09, 2004 2:20 pm
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);
?>