Using Default Arguments in a Function
I would propose changing the function declaration as follows so you can do what you want:
function foo($blah, $x = null, $y = null) {
if (null === $x) {
$x = "some value";
}
if (null === $y) {
$y = "some other value";
}
code here!
}
This way, you can make a call like foo('blah', null, 'non-default y value');
and have it work as you want, where the second parameter $x
still gets its default value.
With this method, passing a null value means you want the default value for one parameter when you want to override the default value for a parameter that comes after it.
As stated in other answers,
default parameters only work as the last arguments to the function.
If you want to declare the default values in the function definition,
there is no way to omit one parameter and override one following it.
If I have a method that can accept varying numbers of parameters, and parameters of varying types, I often declare the function similar to the answer shown by Ryan P.
Here is another example (this doesn't answer your question, but is hopefully informative:
public function __construct($params = null)
{
if ($params instanceof SOMETHING) {
// single parameter, of object type SOMETHING
} elseif (is_string($params)) {
// single argument given as string
} elseif (is_array($params)) {
// params could be an array of properties like array('x' => 'x1', 'y' => 'y1')
} elseif (func_num_args() == 3) {
$args = func_get_args();
// 3 parameters passed
} elseif (func_num_args() == 5) {
$args = func_get_args();
// 5 parameters passed
} else {
throw new \InvalidArgumentException("Could not figure out parameters!");
}
}
php default arguments
You can use either:
example($argument1, '', 'test');
Or
example($argument1, NULL, 'test');
You can always check for NULL using instead of empty string:
if ($argument === NULL) {
//
}
I think it all depends on what happens inside the function with the args.
How to tell PHP to use default parameter in a function call?
Put the default parameters at the end, then don't fill in the parameter in the function call.
example:
function nums($c, $a = 1, $b = 2){
echo "$a, $b, $c";
}
nums(3);
Default parameters can be overridden by adding them to the function call:
function nums($c, $a = 1, $b = 2){
echo "$a, $b, $c";
}
nums(3, 12, 27);
PHP default argument function call
Well, the short answer is that there is no better way to do it, to my knowledge. You can, however, make it look somewhat neater by using ternary operators.
function func3_name(arg1,arg2=null,etc...) {
arg2 = (arg2==null ? time() : arg2);
}
How do you create optional arguments in php?
Much like the manual, use an equals (=
) sign in your definition of the parameters:
function dosomething($var1, $var2, $var3 = 'somevalue'){
// Rest of function here...
}
PHP pass default argument to function
This is not natively possible in PHP. There are workarounds like using arrays to pass all parameters instead of a row of arguments, but they have massive downsides.
The best manual workaround that I can think of is defining a constant with an arbitrary value that can't collide with a real value. For example, for a parameter that can never be -1
:
define("DEFAULT_ARGUMENT", -1);
and test for that:
function($foo = DEFAULT_ARGUMENT, $bar = false){}
how to pass current year as default argument in php function
You can't use a function for a default argument value :
The default value must be a constant expression, not (for example) a variable, a class member or a function call.
(extract from PHP doc)
You should set the default value into the function :
function myFunction($month, $year = null)
{
if(!(bool)$year) {
$year = date('Y');
}
echo $year.', '.$month;
}
myFunction('June', '2006'); // 2006, June
myFunction(3, 2010); // 2010, 3
myFunction('July'); // 2013, July
PHP - Default function argument to new object
I'll post an answer as I think it's clear enough.
From the docs, section "Example #6 Using non-scalar types as default values section":
The default value must be a constant expression, not (for example) a
variable, a class member or a function call.
So no, you can't use an object as a default value. Your final example seems to be the best you can do.
As mentioned in the comments above, I'd recommend making the variable nullable to avoid fatal errors if someone explicitly passes a null.
function myFunction(string $string, ?Collection $collection = null){
$collection = is_null($collection) ? new Collection : $collection;
}
php function default argument delivers bool
Apparently the second parameter gets filled up by the CI_Form_validation as a bool.
So to fix this issue in codeigniter.
$this->form_validation->set_rules('aanvangDatum','Aanvangs datum','trim|min_length[10]|max_length[10]|valid_date[d-m-Y]');
The parameter is the d-m-Y between the []
Related Topics
Pdo Were Rows Affected During Execute Statement
How to Use Perl Libraries from PHP
How to Read a .Txt File with PHP
How to Add Two Strings Together
How to Remove <Br /> Tags and More from a String
Parse Select Clause of SQL Queries into a PHP Array
How to Deploy Correctly When Using Composer's Develop/Production Switch
What's the Best Way to Create a Single-File Upload Form Using PHP
Difference Between MySQL_Fetch_Array and MySQL_Fetch_Row
How to Ipc Between PHP Clients and a C Daemon Server
How to Keep Whitespace Formatting Using PHP/Html
Simplexml Soap Response Namespace Issues
PHP File_Get_Contents Does Not Work on Localhost
Default as First Option in Switch Statement
Override External Product Url to "Add to Cart" Product Button
Getting the Difference Between Two Time/Dates Using PHP