0

So I'd like to be able to switch a certain function on or off depending on if a variable is true or false. The function would still execute, but because the variable is false, it won't do it's routine.

Something like this

$testTF = "T";

function Test($test=$testTF){
echo $test;
}

Test("Go");

Of course this won't work, but I'm wondering if there's a way to make it work like intended. This function is sprinkled everywhere in the script, and needs to be in certain places, so I can't just group them all together under one if statement because it would break them.

I know you could just pass the value as a flag every time you call it, but I'm looking for a way to make it assume that value as default unless told otherwise. Maybe I'm looking at globals here but that's dangerous I'm told.

Thanks

7
  • @Dagon: Lol... (you weren't serious, right?) Commented May 24, 2012 at 4:16
  • Im not PHP developer but for logical comparison isn't $test=$testTF should be $test==$testTF Commented May 24, 2012 at 4:18
  • 1
    The = in the params tells it to set it to some default value if not specified Commented May 24, 2012 at 4:19
  • @xbonez sets default value, which is what I read that he wanted. Commented May 24, 2012 at 4:20
  • @Dagon: So, if the default value changes, you expect him to go and change the values every place he is using it? It goes against the principle of DRY and it error-prone. Further, he could be pulling the value from a DB or getting it via user input Commented May 24, 2012 at 4:21

3 Answers 3

2

Could you do this?

$testTF = "T";

function Test($test=NULL){
    if ($test ===NULL) {
        global $testTF;
        $test = $testTF;
    }
    echo $test;
}

Test("Go");

Optionally, if you're against using globals, you can encapsulate the default value in a function, like this:

function Test($test=NULL){
    if ($test ===NULL) {        
        $test = getDefault();
    }
    echo $test;
}

Test("Go");

function getDefault(){
    //pull default value from wherever
    $testDefault = 'T';
    return $testDefault;
}
Sign up to request clarification or add additional context in comments.

8 Comments

Hey thanks but the problem there is it can't see the testTF var from in the function. It doesn't crash though :)
@user1159454: You should definitely be able to see $testF in the function. Have you defined it in the same file, or do you mean you want to access the variable through various files?
Yes it's all the same file. In PHP I know you can't see variables that aren't either set inside it, or passed as a parameter. I know in javascript that would work
@user1159454: You have to declare $testTF as global first: global $testTF; or use $GLOBALS['testTF'].
@user1159454: Oh, I get you now. See edit.
|
0

Short answer is the way you want to do it is impossible but this is also a duplicate of : PHP function with variable as default value for a parameter

Has the answer you were looking for :)

Comments

0

You could also do this with closures:

$testTF = 'T';

$Test = function ($test = null) use ($testTF){
    if ($test === null) $test = $testTF;
    echo $test;
};

$Test(); //T
$Test(null); //T
$Test('Go'); //Go

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.