Question about passing information to a function

Hi all!

I’m trying to work on a little project here and I have to verify a few variables to change their initial value (“Y” or “N”) to “Actif” or “Inactif” (aka “enabled” / “disabled” in English) depending on the initial value to echo them later on.

Since I have many of them I wanted to create a function that would help me doing this whole thing dynamically.

Right now it goes like this for each of them:
[php]if ($cfu == “Y”) $cfu = “Actif”;
if ($cfu == “N”) $cfu = “Inactif”;[/php]

I have over 10 variables that are being verified like that for now but it will grow bigger.

So what I’ve tried is to create a function (very sloppy results hehe):

[php]
function yesNo($var) {
echo “$var”;
}
yesNo($cfu);

echo result is : Y

OR

function yesNo($var) {
echo “$var”;
}
yesNo(cfu);

echo result is: cfu

[/php]

Problem is that when the function is called, it replaces $cfu by it’s value, so $var returns the value of the $cfu variable (currently Y or N), not the variable name. I can’t seem to find a way to get the variable name passed through. If only I could “ln -s” those 2 variables hehe.

I’ve tried going yesNo(cfu) but then $var returns “cfu” only, and I can’t convert that so I can use it as a variable. Is there a way to do what I need to do?

Thanks for your help

Francois

I’ve finally gotten this function to work, but please tell me if this is the good solution.

$genre is for a future option that I’ll be using so please ignore that.

At least I can get my $cfu variable to be changed by the function (and many others)

[php]function yesNo($var,$genre) {
global $$var;
if ($$var == “Y”) $$var = “Actif”;
if ($$var == “N”) $$var = “Inactif”;
}
echo “$cfu”; # echo result: Y
call_user_func(‘yesNo’, “cfu”, “m”);
echo “$cfu”; # echo result: Actif
[/php]

Hi there,

I’m not entirely sure I am fully understanding the question, but does this help:

[php]
function yesorno($variable)
{
return (strtolower(trim($variable)) == “y”) ? “Actif” : “Inactif”;
}
$cfu = yesorno($cfu);
[/php]

That works even better, and is much simpler, thanks Smokey!

Why didn’t I think of assigning the result of a function to the variable by myself… aah I’m so newbie :slight_smile:

Haha, no problem. We all miss the obvious sometimes - glad to have helped out.

Sponsor our Newsletter | Privacy Policy | Terms of Service