I want to pass the current Year as one of the default value of a function
I AM using
date("Y")
for current year
So it work fine when i write function in this way:
function MYfUNCTION($month,$year = 2013)
{
}
But I want to pass current year instead of 2013 but it gives me an error when i write like this
function MYfUNCTION($month,$year = date("Y"))
{
}
Please help me
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.
PHP Function ArgumentsArguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
The default arguments must be constant expressions. They cannot be variables or function calls. PHP allows you to use a scalar value, an array, and null as the default arguments.
The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.
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
You can try this,
function MYfUNCTION($month,$year ="")
{
if(empty($year)){
$year = date($year); //Output: current year
}
echo "Year: " . $year ." Month: " . $month;
}
No direct method to pass arguments directly in the pre-defined format, you have to pass function putting the year you want to and you can make 1 more function in which you can change the year. like in php get curr year func. !! hope this can help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With