Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8702699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:43:10+00:00 2026-06-13T02:43:10+00:00

Doing a bit of coding, and wanted to set a default value of today’s

  • 0

Doing a bit of coding, and wanted to set a default value of today’s date for an argument in a function that I have. However, having a bit of problems having it work and have it still be dynamic. This is current setup, where I have static values assigned to certain function arguments.

private function search($text, $startDate = "2012-10-19", $endDate = "2012-10-20") {
//code goes here
}

Not what I want, but it’s what works, and the IDE doesnt’ complain.

This is what I’ve tried, with the corresponding complaints from the interpreter

private function search($text, $startDate = $this->getCurrentDate(), $endDate = "2012-10-20") {
//code goes here
}

returns “syntax error, unexpected $this”, where getCurrentDate refers to a private function that only returns a string. Same co-occurrence happens when I call a variable declared in class scope (minus the brackets at the end of getCurrentDate of course). Utilizing static brings up “Undefined class constant ‘self::getCurrentDate ‘ “, regardless of whether I call it as a function or a class scope variable which is odd since I’ve defined it as such.

    private static function getDate() {
    return "foo";
}

and

private static $getTodaysDate = date("M-d-Y", mktime(0, 0, 0, date("M"), date("d"), date("Y")));

in my two different attempts. Of course this

private function search($text, $startDate = date("M-d-Y", mktime(0, 0, 0, date("M"), date("d"), date("Y"))), $endDate = "2012-10-20") {
//code goes here
}

doesn’t work at all.

So I’m sure it’s just some obvious thing I’m missing, but I cannot see why PHP is not allowing me to do this without declaring a static string, or if I’m running against a limitation of the language. Anyone have any ideas as to what’s the cause?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:43:11+00:00Added an answer on June 13, 2026 at 2:43 am

    Default values for function parameters can not be dynamic!
    From the PHP Manual on default values for function parameters:

    The default value must be a constant expression, not (for example) a
    variable, a class member or a function call.

    What you could do is set the default parameter to null and then in your function check if the parameter is null and if so overwrite it with the current date.

    private function search($text, $startDate = null, $endDate = "2012-10-20") {
        if ($startDate === null) $startDate = date("M-d-Y", mktime(0, 0, 0, date("M"), date("d"), date("Y"));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a bit of coding, where I have to write this sort of
In doing a bit more programming with Firebase today, I found myself wishing for
I was doing a bit research and I came across this comment. I have
My first post here :) I have a summer job doing a bit of
Recently I've been doing lots of weekend coding, and have began to really need
I have been doing several hours of research on a topic that I thought
I'm doing a bit of Powershell scripting ( for the first time ) to
I am doing a bit of Office Automation and when printing a .doc file
How would you go about doing a bit shift in NASM on a register?
Ok, so I've been doing a bit of research into NoSQL databases, and they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.