Jump to content

Using a variable as an argument's default value..


Slips

Recommended Posts

Hello, i know this is a very silly question but it bothers me to know the reasoning behind it. My application has a design where the functions are stored in functions.php and variables are stored in variables.php. There are some functions that have variables that i might change often and the reason i don't directly use them in the argument itself is so that in future if i have to change the default argument's value , then i wouldn't have to scroll through all the functions and look for it. I have a slippery memory i know i will be able to remember it better if i store custom global variables separately and custom global functions separately.

 

I tried searching these forums for this but its giving me a message like "search daemon not found" or something like that..

 

Anyways , coming to the point, i know that PHP doesn't accept variables as default arguments but may i know the reasoning behind this? If i understand the reasoning behind this i know it will stick better in my head.So..why does a function not allow a variable as a default value?

 

Or if anyone has a link to explaining this behaviour in PHP, i would appreciate it if you could pass it on!

 

Thank you :)

Link to comment
Share on other sites

I am not sure what you mean by:

a function not allow a variable as a default value

 

could you give a small example?

 

Sorry i meant a function will not allow a variable as a default value for an argument

 

$defaultValue = 6;
function buyGuitar($noOfStrings = $defaultValue) //PHP won't allow a variable as a default argument {
echo 'You have chosen to buy a '.$defaultNoOfStrings.' string guitar!';
}

buyGuitar();


 

And why can't i modify my original message anymore? Is this a part of the forums or a bug?

 

Link to comment
Share on other sites

it wouldn't do there as your assigning the value:

 

function buyGuitar($noOfStrings = $defaultValue)<< = means assign == means evaluate === means type is identical

 

function myfunction(Array $yourArray) << this would throw an error if anything was passed to it other than an array

 

Other types are object, int, string etc.

 

Rw

Link to comment
Share on other sites

Like this:

 

$defaultValue = 6;
function buyGuitar($noOfStrings = null){
    if($noOfStrings == null)
        global $defaultValue;
    else
        $defaultValue = $noOfStrings;
    return 'You have chosen to buy a '.$defaultValue.' string guitar!';
}

echo buyGuitar().'<br>';
echo buyGuitar($defaultValue).'<br>';
echo buyGuitar(20).'<br>';

Link to comment
Share on other sites

Well, here is MY answer to the original question.

 

The reason why you can't use a variable as the default value for a function is because of variable scope. A variable in a function is a separate object than the variable elsewhere in your code. You can define a variable as global within a function, but you can't do that in the parameter definition of the function.

 

Here is how I would set up a function so that a variable will default to a global variable if the value is not passed in the function

$foo = "bar";

function test($foo = false)
{
    if(!$foo)
    {
        global $foo;
    }
    echo $foo . "<br />\n";
}

test(); //Ouput: bar
test('abc'); //Output abc

 

Another solution would be to create your functions in a class and set a variable withint he class that can be used as the default.

Link to comment
Share on other sites

@AbraCadaver , The Little Guy :

 

I know those methods will work, infact i will probably end up doing one of those itself but i just wanted to get an understanding of why you can't pass variables as default argument values.

 

@mjdamato :

 

Yep i have a basic awareness of variable scope but the way i looked at it was, if the function can accept variables from the global scope as arguments while its being called, why not accept variables to be used as default argument values aswell? Does the way it is currently implemented have something to do with the way the compiler/execution works? Sorry about these basic questions, i am studying programming from scratch without studying science subjects in college..

 

Also can anyone recommend some good books that explain the workings of the PHP compiler/executor/backend? i.e stuff that is not explained about in the manual, thanks again :)

Link to comment
Share on other sites

Yep i have a basic awareness of variable scope but the way i looked at it was, if the function can accept variables from the global scope as arguments while its being called, why not accept variables to be used as default argument values aswell?

 

When you pass values TO a function you are passing the values within the scope of where you called the function. So, if the function is called in the global scope (i.e. not in a function or within a different function where the variable has been declared 'global') then you are passing the global value - not the global variable.

 

The function being called doesn't "know" where the value came from. However, once the function is called you are now running in a different scope. So, when you try to "use" a variable in the function it is within the scope of the function - unless you declare it to be global. I know of no way to declare a parameters in a function to be global.

 

To be honest, this is a pretty worthless conversation. The whole process seems to make sense to me. Even so, multiple solutions have been provided where you can use a global value as the default in a function - you just can't do it within the parameter definition of a function.

Link to comment
Share on other sites

@AbraCadaver , The Little Guy :

 

I know those methods will work, infact i will probably end up doing one of those itself but i just wanted to get an understanding of why you can't pass variables as default argument values.

 

@mjdamato :

 

Yep i have a basic awareness of variable scope but the way i looked at it was, if the function can accept variables from the global scope as arguments while its being called, why not accept variables to be used as default argument values aswell? Does the way it is currently implemented have something to do with the way the compiler/execution works? Sorry about these basic questions, i am studying programming from scratch without studying science subjects in college..

 

Also can anyone recommend some good books that explain the workings of the PHP compiler/executor/backend? i.e stuff that is not explained about in the manual, thanks again :)

 

From the manual "The default value must be a constant expression, not (for example) a variable, a class member or a function call. " : http://www.php.net/manual/en/functions.arguments.php#functions.arguments.default

 

I'm not aware of any programming language where you can use a non-constant as a default value for a function argument.  A function declaration is a static declaration at parse/compile time.  A variable is, well... variable.

Link to comment
Share on other sites

Using a variable as an argument's default value..

 

^^^ That's generally referred to as using the argument itself.

 

It sounds like you should not be using a default for the argument in question. You are expecting your function definition to perform logic that your application code should be doing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.