PHP function with variable as default value for a parameter
Solution 1
No, this isn't possible, as stated on the Function arguments manual page:
The default value must be a constant expression, not (for example) a variable, a class member or a function call.
Instead you could either simply pass in null as the default and update this within your function...
function actionOne($id=null) {
$id = isset($id) ? $id : $_GET['ID'];
....
}
...or (better still), simply provide $_GET['ID'] as the argument value when you don't have a specific ID to pass in. (i.e.: Handle this outside the function.)
Solution 2
function actionOne( $id=null ) {
if ($id === null) $id = $_GET['ID'];
}
But, i would probably do this outside of the function:
// This line would change, its just a for instance
$id = $id ? $id : $_GET['id'];
actionOne( $id );
Solution 3
You should get that id before you call the function. Checking for the existence of the parameter breaks encapsulation. You should do something like that:
if (isset($_GET["ID"])
{
$id = $_GET["ID"];
}
else
{
//$id = something else
}
function doSomethingWithID($id)
{
//do something
}
Solution 4
You could use constant variable
define('ID',$_GET["ID"]);
function($id = _ID_){
//code
}
Solution 5
Easy peanuts!
(Might contain minor mistakes, errors or typos!)
You need a helper function, which will call you main function recursively, but having NULL as default:
Wrong: function actionOne($id=$_GET["ID"])
Right:
function actionOne($id) {...}
function actionOnewithID($id=NULL) {
if (NULL==$id){actionOne($_GET["ID"]);}
else {actionOne($id);
}
And if you need to return a value:
function actionOne($id) {...}
function actionOnewithID($id=NULL) {
if (NULL==$id){return(actionOne($_GET["ID"]));}
else {return(actionOne($id));
}
I hope this helps!
The Bndr
Updated on May 24, 2020Comments
-
The Bndr about 4 years
By default a PHP function uses
$_GET
variables. Sometimes this function should be called in an situation where$_GET
is not set. In this case I will define the needed variables as parameter like:actionOne(234)
To get an abstract code I tried something like this:
function actionOne($id=$_GET["ID"])
which results in an error:
Parse error: syntax error, unexpected T_VARIABLE
Is it impossible to define an default parameter by using an variable?
Edit
The
actionOne
is called "directly" from an URL using the framework Yii. By handling the$_GET
variables outside this function, I had to do this on an central component (even it is a simple, insignificant function) or I have to change the framework, what I don't like to do.An other way to do this could be an dummy function (something like an pre-function), which is called by the URL. This "dummy" function handles the variable-issue and calls the
actionOne($id)
. -
The Bndr about 13 yearshi thank you.... there is no other way? ...without handling this problem inside or outside this function?
-
markus about 13 yearsSame here, the solution breaks encapsulation, the check should happen outside the function.
-
markus about 13 yearsa static variable? and you're passing a string? how does that solve the problem?
-
markus about 13 yearsThe only thing which is called a dynamic variable in PHP afaik is a variable-variable and that wouldn't solve the problem either.
-
markus about 13 yearsYour solution is wrong, you're passing a string as default value, how does that address the original question?
-
markus about 13 yearsI still think you should provide the good practice as the main example and the worse practice as the alternative!
-
The Bndr about 13 yearsI updated my post. In my specific case, your solution looks like the best way... (I will check this out soon) In general I probably would use the solution form @Galen. What do you mean with "simply provide $_GET['ID'] as the argument value"? Is this according to your example?
-
The Bndr about 13 yearsisn't it better to use "$id===null?$id=$_GET["ID"]:null;" instead of $id = isset($id) ? $id : $_GET['ID'];? Because you avoid one assignment of $id, if $id is still set ($id=$id)!?! ..or is there no different?
-
John Parker about 13 years@The Bndr I meant that you'd assign $id before you call the function, rather than in the function itself. In terms of your second point - if you're worried about the overhead of a variable assignment, I suspect you shouldn't be using PHP. :-)
-
The Bndr about 13 years@middaparka maybe! ;-) but my version of assigning $id is also usable?! Or is your version the "better practice"?
-
John Parker about 13 years@The Bndr I think mine is probably closer to the tradition "form" that a ternary operator generally takes (as the assignment is explicit), but yours will work fine. It's just a matter of which you think is more readable/easy to comprehend to be honest.